home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / emacs-complete / fsf / emacs / src / xfns.c < prev    next >
C/C++ Source or Header  |  1994-10-23  |  126KB  |  4,550 lines

  1. /* Functions for the X window system.
  2.    Copyright (C) 1989, 1992, 1993, 1994 Free Software Foundation.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Completely rewritten by Richard Stallman.  */
  21.  
  22. /* Rewritten for X11 by Joseph Arceneaux */
  23.  
  24. #include <signal.h>
  25. #include <config.h>
  26.  
  27. #if 0
  28. #include <stdio.h>
  29. #endif
  30.  
  31. /* This makes the fields of a Display accessible, in Xlib header files.  */
  32. #define XLIB_ILLEGAL_ACCESS
  33.  
  34. #include "lisp.h"
  35. #include "xterm.h"
  36. #include "frame.h"
  37. #include "window.h"
  38. #include "buffer.h"
  39. #include "dispextern.h"
  40. #include "keyboard.h"
  41. #include "blockinput.h"
  42.  
  43. #ifdef HAVE_X_WINDOWS
  44. extern void abort ();
  45.  
  46. #ifndef VMS
  47. #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work.  */
  48. #include "bitmaps/gray.xbm"
  49. #else
  50. #include <X11/bitmaps/gray>
  51. #endif
  52. #else
  53. #include "[.bitmaps]gray.xbm"
  54. #endif
  55.  
  56. #ifdef USE_X_TOOLKIT
  57. #include <X11/Shell.h>
  58.  
  59. #include <X11/Xaw/Paned.h>
  60. #include <X11/Xaw/Label.h>
  61.  
  62. #ifdef USG
  63. #undef USG    /* ####KLUDGE for Solaris 2.2 and up */
  64. #include <X11/Xos.h>
  65. #define USG
  66. #else
  67. #include <X11/Xos.h>
  68. #endif
  69.  
  70. #include "widget.h"
  71.  
  72. #include "../lwlib/lwlib.h"
  73.  
  74. /* The one and only application context associated with the connection
  75.    to the one and only X display that Emacs uses.  */
  76. XtAppContext Xt_app_con;
  77.  
  78. /* The one and only application shell.  Emacs screens are popup shells of this
  79.    application.  */
  80. Widget Xt_app_shell;
  81.  
  82. extern void free_frame_menubar ();
  83. extern void free_frame_menubar ();
  84. #endif /* USE_X_TOOLKIT */
  85.  
  86. #define min(a,b) ((a) < (b) ? (a) : (b))
  87. #define max(a,b) ((a) > (b) ? (a) : (b))
  88.  
  89. #ifdef HAVE_X11
  90. /* X Resource data base */
  91. static XrmDatabase xrdb;
  92.  
  93. /* The class of this X application.  */
  94. #define EMACS_CLASS "Emacs"
  95.  
  96. #ifdef HAVE_X11R4
  97. #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
  98. #else
  99. #define MAXREQUEST(dpy) ((dpy)->max_request_size)
  100. #endif
  101.  
  102. /* The name we're using in resource queries.  */
  103. Lisp_Object Vx_resource_name;
  104.  
  105. /* Title name and application name for X stuff. */
  106. extern char *x_id_name;
  107.  
  108. /* The background and shape of the mouse pointer, and shape when not
  109.    over text or in the modeline. */
  110. Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
  111. /* The shape when over mouse-sensitive text.  */
  112. Lisp_Object Vx_sensitive_text_pointer_shape;
  113.  
  114. /* Color of chars displayed in cursor box. */
  115. Lisp_Object Vx_cursor_fore_pixel;
  116.  
  117. /* The screen being used.  */
  118. static Screen *x_screen;
  119.  
  120. /* The X Visual we are using for X windows (the default) */
  121. Visual *screen_visual;
  122.  
  123. /* Height of this X screen in pixels. */
  124. int x_screen_height;
  125.  
  126. /* Width of this X screen in pixels. */
  127. int x_screen_width;
  128.  
  129. /* Number of planes for this screen. */
  130. int x_screen_planes;
  131.  
  132. /* Non nil if no window manager is in use. */
  133. Lisp_Object Vx_no_window_manager;
  134.  
  135. /* `t' if a mouse button is depressed. */
  136.  
  137. Lisp_Object Vmouse_depressed;
  138.  
  139. extern unsigned int x_mouse_x, x_mouse_y, x_mouse_grabbed;
  140.  
  141. /* Atom for indicating window state to the window manager. */
  142. extern Atom Xatom_wm_change_state;
  143.  
  144. /* Communication with window managers. */
  145. extern Atom Xatom_wm_protocols;
  146.  
  147. /* Kinds of protocol things we may receive. */
  148. extern Atom Xatom_wm_take_focus;
  149. extern Atom Xatom_wm_save_yourself;
  150. extern Atom Xatom_wm_delete_window;
  151.  
  152. /* Other WM communication */
  153. extern Atom Xatom_wm_configure_denied; /* When our config request is denied */
  154. extern Atom Xatom_wm_window_moved;     /* When the WM moves us. */
  155.  
  156. /* EditRes protocol */
  157. extern Atom Xatom_editres_name;
  158.  
  159. #else    /* X10 */
  160.  
  161. /* Default size of an Emacs window.  */
  162. static char *default_window = "=80x24+0+0";
  163.  
  164. #define MAXICID 80
  165. char iconidentity[MAXICID];
  166. #define ICONTAG "emacs@"
  167. char minibuffer_iconidentity[MAXICID];
  168. #define MINIBUFFER_ICONTAG "minibuffer@"
  169.  
  170. #endif /* X10 */
  171.  
  172. /* The last 23 bits of the timestamp of the last mouse button event. */
  173. Time mouse_timestamp;
  174.  
  175. /* Evaluate this expression to rebuild the section of syms_of_xfns
  176.    that initializes and staticpros the symbols declared below.  Note
  177.    that Emacs 18 has a bug that keeps C-x C-e from being able to
  178.    evaluate this expression.
  179.  
  180. (progn
  181.   ;; Accumulate a list of the symbols we want to initialize from the
  182.   ;; declarations at the top of the file.
  183.   (goto-char (point-min))
  184.   (search-forward "/\*&&& symbols declared here &&&*\/\n")
  185.   (let (symbol-list)
  186.     (while (looking-at "Lisp_Object \\(Q[a-z_]+\\)")
  187.       (setq symbol-list
  188.         (cons (buffer-substring (match-beginning 1) (match-end 1))
  189.           symbol-list))
  190.       (forward-line 1))
  191.     (setq symbol-list (nreverse symbol-list))
  192.     ;; Delete the section of syms_of_... where we initialize the symbols.
  193.     (search-forward "\n  /\*&&& init symbols here &&&*\/\n")
  194.     (let ((start (point)))
  195.       (while (looking-at "^  Q")
  196.     (forward-line 2))
  197.       (kill-region start (point)))
  198.     ;; Write a new symbol initialization section.
  199.     (while symbol-list
  200.       (insert (format "  %s = intern (\"" (car symbol-list)))
  201.       (let ((start (point)))
  202.     (insert (substring (car symbol-list) 1))
  203.     (subst-char-in-region start (point) ?_ ?-))
  204.       (insert (format "\");\n  staticpro (&%s);\n" (car symbol-list)))
  205.       (setq symbol-list (cdr symbol-list)))))
  206.  
  207.   */        
  208.  
  209. /*&&& symbols declared here &&&*/
  210. Lisp_Object Qauto_raise;
  211. Lisp_Object Qauto_lower;
  212. Lisp_Object Qbackground_color;
  213. Lisp_Object Qbar;
  214. Lisp_Object Qborder_color;
  215. Lisp_Object Qborder_width;
  216. Lisp_Object Qbox;
  217. Lisp_Object Qcursor_color;
  218. Lisp_Object Qcursor_type;
  219. Lisp_Object Qfont;
  220. Lisp_Object Qforeground_color;
  221. Lisp_Object Qgeometry;
  222. /* Lisp_Object Qicon; */
  223. Lisp_Object Qicon_left;
  224. Lisp_Object Qicon_top;
  225. Lisp_Object Qicon_type;
  226. Lisp_Object Qinternal_border_width;
  227. Lisp_Object Qleft;
  228. Lisp_Object Qmouse_color;
  229. Lisp_Object Qnone;
  230. Lisp_Object Qparent_id;
  231. Lisp_Object Qsuppress_icon;
  232. Lisp_Object Qtop;
  233. Lisp_Object Qundefined_color;
  234. Lisp_Object Qvertical_scroll_bars;
  235. Lisp_Object Qvisibility;
  236. Lisp_Object Qwindow_id;
  237. Lisp_Object Qx_frame_parameter;
  238. Lisp_Object Qx_resource_name;
  239. Lisp_Object Quser_position;
  240. Lisp_Object Quser_size;
  241.  
  242. /* The below are defined in frame.c. */
  243. extern Lisp_Object Qheight, Qminibuffer, Qname, Qonly, Qwidth;
  244. extern Lisp_Object Qunsplittable, Qmenu_bar_lines;
  245.  
  246. extern Lisp_Object Vwindow_system_version;
  247.  
  248.  
  249. /* Error if we are not connected to X.  */
  250. void
  251. check_x ()
  252. {
  253.   if (x_current_display == 0)
  254.     error ("X windows are not in use or not initialized");
  255. }
  256.  
  257. /* Nonzero if using X for display.  */
  258.  
  259. int
  260. using_x_p ()
  261. {
  262.   return x_current_display != 0;
  263. }
  264.  
  265. /* Return the Emacs frame-object corresponding to an X window.
  266.    It could be the frame's main window or an icon window.  */
  267.  
  268. /* This function can be called during GC, so use XGCTYPE.  */
  269.  
  270. struct frame *
  271. x_window_to_frame (wdesc)
  272.      int wdesc;
  273. {
  274.   Lisp_Object tail, frame;
  275.   struct frame *f;
  276.  
  277.   for (tail = Vframe_list; XGCTYPE (tail) == Lisp_Cons;
  278.        tail = XCONS (tail)->cdr)
  279.     {
  280.       frame = XCONS (tail)->car;
  281.       if (XGCTYPE (frame) != Lisp_Frame)
  282.         continue;
  283.       f = XFRAME (frame);
  284. #ifdef USE_X_TOOLKIT
  285.       if (f->display.nothing == 1) 
  286.     return 0;
  287.       if ((f->display.x->edit_widget 
  288.        && XtWindow (f->display.x->edit_widget) == wdesc)
  289.           || f->display.x->icon_desc == wdesc)
  290.         return f;
  291. #else /* not USE_X_TOOLKIT */
  292.       if (FRAME_X_WINDOW (f) == wdesc
  293.           || f->display.x->icon_desc == wdesc)
  294.         return f;
  295. #endif /* not USE_X_TOOLKIT */
  296.     }
  297.   return 0;
  298. }
  299.  
  300. #ifdef USE_X_TOOLKIT
  301. /* Like x_window_to_frame but also compares the window with the widget's
  302.    windows.  */
  303.  
  304. struct frame *
  305. x_any_window_to_frame (wdesc)
  306.      int wdesc;
  307. {
  308.   Lisp_Object tail, frame;
  309.   struct frame *f;
  310.   struct x_display *x;
  311.  
  312.   for (tail = Vframe_list; XGCTYPE (tail) == Lisp_Cons;
  313.        tail = XCONS (tail)->cdr)
  314.     {
  315.       frame = XCONS (tail)->car;
  316.       if (XGCTYPE (frame) != Lisp_Frame)
  317.         continue;
  318.       f = XFRAME (frame);
  319.       if (f->display.nothing == 1) 
  320.     return 0;
  321.       x = f->display.x;
  322.       /* This frame matches if the window is any of its widgets.  */
  323.       if (wdesc == XtWindow (x->widget) 
  324.       || wdesc == XtWindow (x->column_widget) 
  325.       || wdesc == XtWindow (x->edit_widget))
  326.     return f;
  327.       /* Match if the window is this frame's menubar.  */
  328.       if (x->menubar_widget 
  329.       && wdesc == XtWindow (x->menubar_widget))
  330.     return f;
  331.     }
  332.   return 0;
  333. }
  334.  
  335. /* Return the frame whose principal (outermost) window is WDESC.
  336.    If WDESC is some other (smaller) window, we return 0.  */
  337.  
  338. struct frame *
  339. x_top_window_to_frame (wdesc)
  340.      int wdesc;
  341. {
  342.   Lisp_Object tail, frame;
  343.   struct frame *f;
  344.   struct x_display *x;
  345.  
  346.   for (tail = Vframe_list; XGCTYPE (tail) == Lisp_Cons;
  347.        tail = XCONS (tail)->cdr)
  348.     {
  349.       frame = XCONS (tail)->car;
  350.       if (XGCTYPE (frame) != Lisp_Frame)
  351.         continue;
  352.       f = XFRAME (frame);
  353.       if (f->display.nothing == 1) 
  354.     return 0;
  355.       x = f->display.x;
  356.       /* This frame matches if the window is its topmost widget.  */
  357.       if (wdesc == XtWindow (x->widget))
  358.     return f;
  359.       /* Match if the window is this frame's menubar.  */
  360.       if (x->menubar_widget 
  361.       && wdesc == XtWindow (x->menubar_widget))
  362.     return f;
  363.     }
  364.   return 0;
  365. }
  366. #endif /* USE_X_TOOLKIT */
  367.  
  368.  
  369. /* Connect the frame-parameter names for X frames
  370.    to the ways of passing the parameter values to the window system.
  371.  
  372.    The name of a parameter, as a Lisp symbol,
  373.    has an `x-frame-parameter' property which is an integer in Lisp
  374.    but can be interpreted as an `enum x_frame_parm' in C.  */
  375.  
  376. enum x_frame_parm
  377. {
  378.   X_PARM_FOREGROUND_COLOR,
  379.   X_PARM_BACKGROUND_COLOR,
  380.   X_PARM_MOUSE_COLOR,
  381.   X_PARM_CURSOR_COLOR,
  382.   X_PARM_BORDER_COLOR,
  383.   X_PARM_ICON_TYPE,
  384.   X_PARM_FONT,
  385.   X_PARM_BORDER_WIDTH,
  386.   X_PARM_INTERNAL_BORDER_WIDTH,
  387.   X_PARM_NAME,
  388.   X_PARM_AUTORAISE,
  389.   X_PARM_AUTOLOWER,
  390.   X_PARM_VERT_SCROLL_BAR,
  391.   X_PARM_VISIBILITY,
  392.   X_PARM_MENU_BAR_LINES
  393. };
  394.  
  395.  
  396. struct x_frame_parm_table
  397. {
  398.   char *name;
  399.   void (*setter)( /* struct frame *frame, Lisp_Object val, oldval */ );
  400. };
  401.  
  402. void x_set_foreground_color ();
  403. void x_set_background_color ();
  404. void x_set_mouse_color ();
  405. void x_set_cursor_color ();
  406. void x_set_border_color ();
  407. void x_set_cursor_type ();
  408. void x_set_icon_type ();
  409. void x_set_font ();
  410. void x_set_border_width ();
  411. void x_set_internal_border_width ();
  412. void x_explicitly_set_name ();
  413. void x_set_autoraise ();
  414. void x_set_autolower ();
  415. void x_set_vertical_scroll_bars ();
  416. void x_set_visibility ();
  417. void x_set_menu_bar_lines ();
  418.  
  419. static struct x_frame_parm_table x_frame_parms[] =
  420. {
  421.   "foreground-color", x_set_foreground_color,
  422.   "background-color", x_set_background_color,
  423.   "mouse-color", x_set_mouse_color,
  424.   "cursor-color", x_set_cursor_color,
  425.   "border-color", x_set_border_color,
  426.   "cursor-type", x_set_cursor_type,
  427.   "icon-type", x_set_icon_type,
  428.   "font", x_set_font,
  429.   "border-width", x_set_border_width,
  430.   "internal-border-width", x_set_internal_border_width,
  431.   "name", x_explicitly_set_name,
  432.   "auto-raise", x_set_autoraise,
  433.   "auto-lower", x_set_autolower,
  434.   "vertical-scroll-bars", x_set_vertical_scroll_bars,
  435.   "visibility", x_set_visibility,
  436.   "menu-bar-lines", x_set_menu_bar_lines,
  437. };
  438.  
  439. /* Attach the `x-frame-parameter' properties to
  440.    the Lisp symbol names of parameters relevant to X.  */
  441.  
  442. init_x_parm_symbols ()
  443. {
  444.   int i;
  445.  
  446.   for (i = 0; i < sizeof (x_frame_parms) / sizeof (x_frame_parms[0]); i++)
  447.     Fput (intern (x_frame_parms[i].name), Qx_frame_parameter,
  448.       make_number (i));
  449. }
  450.  
  451. /* Change the parameters of FRAME as specified by ALIST.
  452.    If a parameter is not specially recognized, do nothing;
  453.    otherwise call the `x_set_...' function for that parameter.  */
  454.  
  455. void
  456. x_set_frame_parameters (f, alist)
  457.      FRAME_PTR f;
  458.      Lisp_Object alist;
  459. {
  460.   Lisp_Object tail;
  461.  
  462.   /* If both of these parameters are present, it's more efficient to
  463.      set them both at once.  So we wait until we've looked at the
  464.      entire list before we set them.  */
  465.   Lisp_Object width, height;
  466.  
  467.   /* Same here.  */
  468.   Lisp_Object left, top;
  469.  
  470.   /* Record in these vectors all the parms specified.  */
  471.   Lisp_Object *parms;
  472.   Lisp_Object *values;
  473.   int i;
  474.   
  475.   i = 0;
  476.   for (tail = alist; CONSP (tail); tail = Fcdr (tail))
  477.     i++;
  478.  
  479.   parms = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
  480.   values = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
  481.  
  482.   /* Extract parm names and values into those vectors.  */
  483.  
  484.   i = 0;
  485.   for (tail = alist; CONSP (tail); tail = Fcdr (tail))
  486.     {
  487.       Lisp_Object elt, prop, val;
  488.  
  489.       elt = Fcar (tail);
  490.       parms[i] = Fcar (elt);
  491.       values[i] = Fcdr (elt);
  492.       i++;
  493.     }
  494.  
  495.   width = height = top = left = Qunbound;
  496.  
  497.   /* Now process them in reverse of specified order.  */
  498.   for (i--; i >= 0; i--)
  499.     {
  500.       Lisp_Object prop, val;
  501.  
  502.       prop = parms[i];
  503.       val = values[i];
  504.  
  505.       if (EQ (prop, Qwidth))
  506.     width = val;
  507.       else if (EQ (prop, Qheight))
  508.     height = val;
  509.       else if (EQ (prop, Qtop))
  510.     top = val;
  511.       else if (EQ (prop, Qleft))
  512.     left = val;
  513.       else
  514.     {
  515.       register Lisp_Object param_index, old_value;
  516.  
  517.       param_index = Fget (prop, Qx_frame_parameter);
  518.       old_value = get_frame_param (f, prop);
  519.       store_frame_param (f, prop, val);
  520.       if (XTYPE (param_index) == Lisp_Int
  521.           && XINT (param_index) >= 0
  522.           && (XINT (param_index)
  523.           < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
  524.         (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
  525.     }
  526.     }
  527.  
  528.   /* Don't die if just one of these was set.  */
  529.   if (EQ (left, Qunbound))
  530.     XSET (left, Lisp_Int, f->display.x->left_pos);
  531.   if (EQ (top, Qunbound))
  532.     XSET (top, Lisp_Int, f->display.x->top_pos);
  533.  
  534.   /* Don't die if just one of these was set.  */
  535.   if (EQ (width, Qunbound))
  536.     XSET (width, Lisp_Int, FRAME_WIDTH (f));
  537.   if (EQ (height, Qunbound))
  538.     XSET (height, Lisp_Int, FRAME_HEIGHT (f));
  539.  
  540.   /* Don't set these parameters these unless they've been explicitly
  541.      specified.  The window might be mapped or resized while we're in
  542.      this function, and we don't want to override that unless the lisp
  543.      code has asked for it.
  544.  
  545.      Don't set these parameters unless they actually differ from the
  546.      window's current parameters; the window may not actually exist
  547.      yet.  */
  548.   {
  549.     Lisp_Object frame;
  550.  
  551.     check_frame_size (f, &height, &width);
  552.  
  553.     XSET (frame, Lisp_Frame, f);
  554.  
  555.     if ((NUMBERP (width) && XINT (width) != FRAME_WIDTH (f))
  556.     || (NUMBERP (height) && XINT (height) != FRAME_HEIGHT (f)))
  557.       Fset_frame_size (frame, width, height);
  558.  
  559.     if ((!NILP (left) || !NILP (top))
  560.     && ! (NUMBERP (left) && XINT (left) == f->display.x->left_pos
  561.           && NUMBERP (top) && XINT (top) == f->display.x->top_pos))
  562.       {
  563.     int leftpos = (NUMBERP (left) ? XINT (left) : 0);
  564.     int toppos = (NUMBERP (top) ? XINT (top) : 0);
  565.  
  566.     /* Store the numeric value of the position.  */
  567.     f->display.x->top_pos = toppos;
  568.     f->display.x->left_pos = leftpos;
  569.  
  570.     /* Record the signs.  */
  571.     f->display.x->size_hint_flags &= ~ (XNegative | YNegative);
  572.     if (EQ (left, Qminus) || (NUMBERP (left) && XINT (left) < 0))
  573.       f->display.x->size_hint_flags |= XNegative;
  574.     if (EQ (top, Qminus) || (NUMBERP (top) && XINT (top) < 0))
  575.       f->display.x->size_hint_flags |= YNegative;
  576.     f->display.x->win_gravity = NorthWestGravity;
  577.  
  578.     /* Actually set that position, and convert to absolute.  */
  579.     x_set_offset (f, leftpos, toppos, 0);
  580.       }
  581.   }
  582. }
  583.  
  584. /* Store the positions of frame F into XPTR and YPTR.
  585.    These are the positions of the containing window manager window,
  586.    not Emacs's own window.  */
  587.  
  588. void
  589. x_real_positions (f, xptr, yptr)
  590.      FRAME_PTR f;
  591.      int *xptr, *yptr;
  592. {
  593.   int win_x = 0, win_y = 0;
  594.   Window child;
  595.  
  596.   /* This is pretty gross, but seems to be the easiest way out of
  597.      the problem that arises when restarting window-managers.  */
  598.  
  599. #ifdef USE_X_TOOLKIT
  600.   Window outer = XtWindow (f->display.x->widget);
  601. #else
  602.   Window outer = f->display.x->window_desc;
  603. #endif
  604.   Window tmp_root_window;
  605.   Window *tmp_children;
  606.   int tmp_nchildren;
  607.  
  608.   x_catch_errors ();
  609.   while (1)
  610.     {
  611.       win_x = 0, win_y = 0;
  612.       XQueryTree (x_current_display, outer, &tmp_root_window,
  613.           &f->display.x->parent_desc,
  614.           &tmp_children, &tmp_nchildren);
  615.       xfree (tmp_children);
  616.  
  617.       /* Find the position of the outside upper-left corner of
  618.      the inner window, with respect to the outer window.  */
  619.       if (f->display.x->parent_desc != ROOT_WINDOW)
  620.     {
  621.       BLOCK_INPUT;
  622.       XTranslateCoordinates (x_current_display,
  623.                    
  624.                  /* From-window, to-window.  */
  625. #ifdef USE_X_TOOLKIT
  626.                  XtWindow (f->display.x->widget),
  627. #else
  628.                  f->display.x->window_desc,
  629. #endif
  630.                  f->display.x->parent_desc,
  631.  
  632.                  /* From-position, to-position.  */
  633.                  0, 0, &win_x, &win_y,
  634.  
  635.                  /* Child of win.  */
  636.                  &child);
  637.       UNBLOCK_INPUT;
  638.  
  639.       win_x += f->display.x->border_width;
  640.       win_y += f->display.x->border_width;
  641.     }
  642.  
  643.       /* It is possible for the window returned by the XQueryNotify
  644.      to become invalid by the time we call XTranslateCoordinates.
  645.      That can happen when you restart some window managers.
  646.      If so, we get an error in XTranslateCoordinates.
  647.      Detect that and try the whole thing over.  */
  648.       if (!x_had_errors_p ())
  649.     break;
  650.     }
  651.   x_uncatch_errors ();
  652.  
  653.   *xptr = f->display.x->left_pos - win_x;
  654.   *yptr = f->display.x->top_pos - win_y;
  655. }
  656.  
  657. /* Insert a description of internally-recorded parameters of frame X
  658.    into the parameter alist *ALISTPTR that is to be given to the user.
  659.    Only parameters that are specific to the X window system
  660.    and whose values are not correctly recorded in the frame's
  661.    param_alist need to be considered here.  */
  662.  
  663. x_report_frame_params (f, alistptr)
  664.      struct frame *f;
  665.      Lisp_Object *alistptr;
  666. {
  667.   char buf[16];
  668.  
  669.   store_in_alist (alistptr, Qleft, make_number (f->display.x->left_pos));
  670.   store_in_alist (alistptr, Qtop, make_number (f->display.x->top_pos));
  671.   store_in_alist (alistptr, Qborder_width,
  672.               make_number (f->display.x->border_width));
  673.   store_in_alist (alistptr, Qinternal_border_width,
  674.               make_number (f->display.x->internal_border_width));
  675.   sprintf (buf, "%d", FRAME_X_WINDOW (f));
  676.   store_in_alist (alistptr, Qwindow_id,
  677.               build_string (buf));
  678.   FRAME_SAMPLE_VISIBILITY (f);
  679.   store_in_alist (alistptr, Qvisibility,
  680.           (FRAME_VISIBLE_P (f) ? Qt
  681.            : FRAME_ICONIFIED_P (f) ? Qicon : Qnil));
  682. }
  683.  
  684. /* Decide if color named COLOR is valid for the display associated with
  685.    the selected frame; if so, return the rgb values in COLOR_DEF.
  686.    If ALLOC is nonzero, allocate a new colormap cell.  */
  687.  
  688. int
  689. defined_color (color, color_def, alloc)
  690.      char *color;
  691.      Color *color_def;
  692.      int alloc;
  693. {
  694.   register int foo;
  695.   Colormap screen_colormap;
  696.  
  697.   BLOCK_INPUT;
  698. #ifdef HAVE_X11
  699.   screen_colormap
  700.     = DefaultColormap (x_current_display, XDefaultScreen (x_current_display));
  701.  
  702.   foo = XParseColor (x_current_display, screen_colormap, color, color_def);
  703.   if (foo && alloc)
  704.     foo = XAllocColor (x_current_display, screen_colormap, color_def);
  705. #else
  706.   foo = XParseColor (color, color_def);
  707.   if (foo && alloc)
  708.     foo = XGetHardwareColor (color_def);
  709. #endif /* not HAVE_X11 */
  710.   UNBLOCK_INPUT;
  711.  
  712.   if (foo)
  713.     return 1;
  714.   else
  715.     return 0;
  716. }
  717.  
  718. /* Given a string ARG naming a color, compute a pixel value from it
  719.    suitable for screen F.
  720.    If F is not a color screen, return DEF (default) regardless of what
  721.    ARG says.  */
  722.  
  723. int
  724. x_decode_color (arg, def)
  725.      Lisp_Object arg;
  726.      int def;
  727. {
  728.   Color cdef;
  729.  
  730.   CHECK_STRING (arg, 0);
  731.  
  732.   if (strcmp (XSTRING (arg)->data, "black") == 0)
  733.     return BLACK_PIX_DEFAULT;
  734.   else if (strcmp (XSTRING (arg)->data, "white") == 0)
  735.     return WHITE_PIX_DEFAULT;
  736.  
  737. #ifdef HAVE_X11
  738.   if (x_screen_planes == 1)
  739.     return def;
  740. #else
  741.   if (DISPLAY_CELLS == 1)
  742.     return def;
  743. #endif
  744.  
  745.   if (defined_color (XSTRING (arg)->data, &cdef, 1))
  746.     return cdef.pixel;
  747.   else
  748.     Fsignal (Qundefined_color, Fcons (arg, Qnil));
  749. }
  750.  
  751. /* Functions called only from `x_set_frame_param'
  752.    to set individual parameters.
  753.  
  754.    If FRAME_X_WINDOW (f) is 0,
  755.    the frame is being created and its X-window does not exist yet.
  756.    In that case, just record the parameter's new value
  757.    in the standard place; do not attempt to change the window.  */
  758.  
  759. void
  760. x_set_foreground_color (f, arg, oldval)
  761.      struct frame *f;
  762.      Lisp_Object arg, oldval;
  763. {
  764.   f->display.x->foreground_pixel = x_decode_color (arg, BLACK_PIX_DEFAULT);
  765.   if (FRAME_X_WINDOW (f) != 0)
  766.     {
  767. #ifdef HAVE_X11
  768.       BLOCK_INPUT;
  769.       XSetForeground (x_current_display, f->display.x->normal_gc,
  770.               f->display.x->foreground_pixel);
  771.       XSetBackground (x_current_display, f->display.x->reverse_gc,
  772.               f->display.x->foreground_pixel);
  773.       UNBLOCK_INPUT;
  774. #endif                /* HAVE_X11 */
  775.       recompute_basic_faces (f);
  776.       if (FRAME_VISIBLE_P (f))
  777.         redraw_frame (f);
  778.     }
  779. }
  780.  
  781. void
  782. x_set_background_color (f, arg, oldval)
  783.      struct frame *f;
  784.      Lisp_Object arg, oldval;
  785. {
  786.   Pixmap temp;
  787.   int mask;
  788.  
  789.   f->display.x->background_pixel = x_decode_color (arg, WHITE_PIX_DEFAULT);
  790.  
  791.   if (FRAME_X_WINDOW (f) != 0)
  792.     {
  793.       BLOCK_INPUT;
  794. #ifdef HAVE_X11
  795.       /* The main frame area. */
  796.       XSetBackground (x_current_display, f->display.x->normal_gc,
  797.               f->display.x->background_pixel);
  798.       XSetForeground (x_current_display, f->display.x->reverse_gc,
  799.               f->display.x->background_pixel);
  800.       XSetForeground (x_current_display, f->display.x->cursor_gc,
  801.               f->display.x->background_pixel);
  802.       XSetWindowBackground (x_current_display, FRAME_X_WINDOW (f),
  803.                 f->display.x->background_pixel);
  804.       {
  805.     Lisp_Object bar;
  806.     for (bar = FRAME_SCROLL_BARS (f); !NILP (bar);
  807.          bar = XSCROLL_BAR (bar)->next)
  808.       XSetWindowBackground (x_current_display,
  809.                 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
  810.                 f->display.x->background_pixel);
  811.       }
  812. #else
  813.       temp = XMakeTile (f->display.x->background_pixel);
  814.       XChangeBackground (FRAME_X_WINDOW (f), temp);
  815.       XFreePixmap (temp);
  816. #endif                /* not HAVE_X11 */
  817.       UNBLOCK_INPUT;
  818.  
  819.       recompute_basic_faces (f);
  820.  
  821.       if (FRAME_VISIBLE_P (f))
  822.         redraw_frame (f);
  823.     }
  824. }
  825.  
  826. void
  827. x_set_mouse_color (f, arg, oldval)
  828.      struct frame *f;
  829.      Lisp_Object arg, oldval;
  830. {
  831.   Cursor cursor, nontext_cursor, mode_cursor, cross_cursor;
  832.   int mask_color;
  833.  
  834.   if (!EQ (Qnil, arg))
  835.     f->display.x->mouse_pixel = x_decode_color (arg, BLACK_PIX_DEFAULT);
  836.   mask_color = f->display.x->background_pixel;
  837.                 /* No invisible pointers. */
  838.   if (mask_color == f->display.x->mouse_pixel
  839.     && mask_color == f->display.x->background_pixel)
  840.     f->display.x->mouse_pixel = f->display.x->foreground_pixel;
  841.  
  842.   BLOCK_INPUT;
  843. #ifdef HAVE_X11
  844.  
  845.   /* It's not okay to crash if the user selects a screwy cursor.  */
  846.   x_catch_errors ();
  847.  
  848.   if (!EQ (Qnil, Vx_pointer_shape))
  849.     {
  850.       CHECK_NUMBER (Vx_pointer_shape, 0);
  851.       cursor = XCreateFontCursor (x_current_display, XINT (Vx_pointer_shape));
  852.     }
  853.   else
  854.     cursor = XCreateFontCursor (x_current_display, XC_xterm);
  855.   x_check_errors ("bad text pointer cursor: %s");
  856.  
  857.   if (!EQ (Qnil, Vx_nontext_pointer_shape))
  858.     {
  859.       CHECK_NUMBER (Vx_nontext_pointer_shape, 0);
  860.       nontext_cursor = XCreateFontCursor (x_current_display,
  861.                       XINT (Vx_nontext_pointer_shape));
  862.     }
  863.   else
  864.     nontext_cursor = XCreateFontCursor (x_current_display, XC_left_ptr);
  865.   x_check_errors ("bad nontext pointer cursor: %s");
  866.  
  867.   if (!EQ (Qnil, Vx_mode_pointer_shape))
  868.     {
  869.       CHECK_NUMBER (Vx_mode_pointer_shape, 0);
  870.       mode_cursor = XCreateFontCursor (x_current_display,
  871.                       XINT (Vx_mode_pointer_shape));
  872.     }
  873.   else
  874.     mode_cursor = XCreateFontCursor (x_current_display, XC_xterm);
  875.   x_check_errors ("bad modeline pointer cursor: %s");
  876.  
  877.   if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
  878.     {
  879.       CHECK_NUMBER (Vx_sensitive_text_pointer_shape, 0);
  880.       cross_cursor
  881.     = XCreateFontCursor (x_current_display,
  882.                  XINT (Vx_sensitive_text_pointer_shape));
  883.     }
  884.   else
  885.     cross_cursor = XCreateFontCursor (x_current_display, XC_crosshair);
  886.  
  887.   /* Check and report errors with the above calls.  */
  888.   x_check_errors ("can't set cursor shape: %s");
  889.   x_uncatch_errors ();
  890.  
  891.   {
  892.     XColor fore_color, back_color;
  893.  
  894.     fore_color.pixel = f->display.x->mouse_pixel;
  895.     back_color.pixel = mask_color;
  896.     XQueryColor (x_current_display,
  897.          DefaultColormap (x_current_display,
  898.                   DefaultScreen (x_current_display)),
  899.          &fore_color);
  900.     XQueryColor (x_current_display,
  901.          DefaultColormap (x_current_display,
  902.                   DefaultScreen (x_current_display)),
  903.          &back_color);
  904.     XRecolorCursor (x_current_display, cursor,
  905.             &fore_color, &back_color);
  906.     XRecolorCursor (x_current_display, nontext_cursor,
  907.             &fore_color, &back_color);
  908.     XRecolorCursor (x_current_display, mode_cursor,
  909.             &fore_color, &back_color);
  910.     XRecolorCursor (x_current_display, cross_cursor,
  911.                     &fore_color, &back_color);
  912.   }
  913. #else /* X10 */
  914.   cursor = XCreateCursor (16, 16, MouseCursor, MouseMask,
  915.               0, 0,
  916.               f->display.x->mouse_pixel,
  917.               f->display.x->background_pixel,
  918.               GXcopy);
  919. #endif /* X10 */
  920.  
  921.   if (FRAME_X_WINDOW (f) != 0)
  922.     {
  923.       XDefineCursor (XDISPLAY FRAME_X_WINDOW (f), cursor);
  924.     }
  925.  
  926.   if (cursor != f->display.x->text_cursor && f->display.x->text_cursor != 0)
  927.       XFreeCursor (XDISPLAY f->display.x->text_cursor);
  928.   f->display.x->text_cursor = cursor;
  929. #ifdef HAVE_X11
  930.   if (nontext_cursor != f->display.x->nontext_cursor
  931.       && f->display.x->nontext_cursor != 0)
  932.       XFreeCursor (XDISPLAY f->display.x->nontext_cursor);
  933.   f->display.x->nontext_cursor = nontext_cursor;
  934.  
  935.   if (mode_cursor != f->display.x->modeline_cursor
  936.       && f->display.x->modeline_cursor != 0)
  937.       XFreeCursor (XDISPLAY f->display.x->modeline_cursor);
  938.   f->display.x->modeline_cursor = mode_cursor;
  939.   if (cross_cursor != f->display.x->cross_cursor
  940.       && f->display.x->cross_cursor != 0)
  941.       XFreeCursor (XDISPLAY f->display.x->cross_cursor);
  942.   f->display.x->cross_cursor = cross_cursor;
  943. #endif    /* HAVE_X11 */
  944.  
  945.   XFlushQueue ();
  946.   UNBLOCK_INPUT;
  947. }
  948.  
  949. void
  950. x_set_cursor_color (f, arg, oldval)
  951.      struct frame *f;
  952.      Lisp_Object arg, oldval;
  953. {
  954.   unsigned long fore_pixel;
  955.  
  956.   if (!EQ (Vx_cursor_fore_pixel, Qnil))
  957.     fore_pixel = x_decode_color (Vx_cursor_fore_pixel, WHITE_PIX_DEFAULT);
  958.   else
  959.     fore_pixel = f->display.x->background_pixel;
  960.   f->display.x->cursor_pixel = x_decode_color (arg, BLACK_PIX_DEFAULT);
  961.   
  962.   /* Make sure that the cursor color differs from the background color.  */
  963.   if (f->display.x->cursor_pixel == f->display.x->background_pixel)
  964.     {
  965.       f->display.x->cursor_pixel = f->display.x->mouse_pixel;
  966.       if (f->display.x->cursor_pixel == fore_pixel)
  967.     fore_pixel = f->display.x->background_pixel;
  968.     }
  969.   f->display.x->cursor_foreground_pixel = fore_pixel;
  970.  
  971.   if (FRAME_X_WINDOW (f) != 0)
  972.     {
  973. #ifdef HAVE_X11
  974.       BLOCK_INPUT;
  975.       XSetBackground (x_current_display, f->display.x->cursor_gc,
  976.               f->display.x->cursor_pixel);
  977.       XSetForeground (x_current_display, f->display.x->cursor_gc,
  978.               fore_pixel);
  979.       UNBLOCK_INPUT;
  980. #endif /* HAVE_X11 */
  981.  
  982.       if (FRAME_VISIBLE_P (f))
  983.     {
  984.       x_display_cursor (f, 0);
  985.       x_display_cursor (f, 1);
  986.     }
  987.     }
  988. }
  989.  
  990. /* Set the border-color of frame F to value described by ARG.
  991.    ARG can be a string naming a color.
  992.    The border-color is used for the border that is drawn by the X server.
  993.    Note that this does not fully take effect if done before
  994.    F has an x-window; it must be redone when the window is created.
  995.  
  996.    Note: this is done in two routines because of the way X10 works.
  997.  
  998.    Note: under X11, this is normally the province of the window manager,
  999.    and so emacs' border colors may be overridden. */
  1000.  
  1001. void
  1002. x_set_border_color (f, arg, oldval)
  1003.      struct frame *f;
  1004.      Lisp_Object arg, oldval;
  1005. {
  1006.   unsigned char *str;
  1007.   int pix;
  1008.  
  1009.   CHECK_STRING (arg, 0);
  1010.   str = XSTRING (arg)->data;
  1011.  
  1012. #ifndef HAVE_X11
  1013.   if (!strcmp (str, "grey") || !strcmp (str, "Grey")
  1014.       || !strcmp (str, "gray") || !strcmp (str, "Gray"))
  1015.     pix = -1;
  1016.   else
  1017. #endif /* X10 */
  1018.  
  1019.     pix = x_decode_color (arg, BLACK_PIX_DEFAULT);
  1020.  
  1021.   x_set_border_pixel (f, pix);
  1022. }
  1023.  
  1024. /* Set the border-color of frame F to pixel value PIX.
  1025.    Note that this does not fully take effect if done before
  1026.    F has an x-window.  */
  1027.  
  1028. x_set_border_pixel (f, pix)
  1029.      struct frame *f;
  1030.      int pix;
  1031. {
  1032.   f->display.x->border_pixel = pix;
  1033.  
  1034.   if (FRAME_X_WINDOW (f) != 0 && f->display.x->border_width > 0)
  1035.     {
  1036.       Pixmap temp;
  1037.       int mask;
  1038.  
  1039.       BLOCK_INPUT;
  1040. #ifdef HAVE_X11
  1041.       XSetWindowBorder (x_current_display, FRAME_X_WINDOW (f),
  1042.                 pix);
  1043. #else
  1044.       if (pix < 0)
  1045.         temp = XMakePixmap ((Bitmap) XStoreBitmap (gray_width, gray_height,
  1046.                            gray_bits),
  1047.                     BLACK_PIX_DEFAULT, WHITE_PIX_DEFAULT);
  1048.       else
  1049.         temp = XMakeTile (pix);
  1050.       XChangeBorder (FRAME_X_WINDOW (f), temp);
  1051.       XFreePixmap (XDISPLAY temp);
  1052. #endif /* not HAVE_X11 */
  1053.       UNBLOCK_INPUT;
  1054.  
  1055.       if (FRAME_VISIBLE_P (f))
  1056.         redraw_frame (f);
  1057.     }
  1058. }
  1059.  
  1060. void
  1061. x_set_cursor_type (f, arg, oldval)
  1062.      FRAME_PTR f;
  1063.      Lisp_Object arg, oldval;
  1064. {
  1065.   if (EQ (arg, Qbar))
  1066.     FRAME_DESIRED_CURSOR (f) = bar_cursor;
  1067.   else
  1068. #if 0
  1069.     if (EQ (arg, Qbox))
  1070. #endif
  1071.       FRAME_DESIRED_CURSOR (f) = filled_box_cursor;
  1072.   /* Error messages commented out because people have trouble fixing
  1073.      .Xdefaults with Emacs, when it has something bad in it.  */
  1074. #if 0
  1075.   else
  1076.     error
  1077.       ("the `cursor-type' frame parameter should be either `bar' or `box'");
  1078. #endif
  1079.  
  1080.   /* Make sure the cursor gets redrawn.  This is overkill, but how
  1081.      often do people change cursor types?  */
  1082.   update_mode_lines++;
  1083. }
  1084.  
  1085. void
  1086. x_set_icon_type (f, arg, oldval)
  1087.      struct frame *f;
  1088.      Lisp_Object arg, oldval;
  1089. {
  1090.   Lisp_Object tem;
  1091.   int result;
  1092.  
  1093.   if (EQ (oldval, Qnil) == EQ (arg, Qnil))
  1094.     return;
  1095.  
  1096.   BLOCK_INPUT;
  1097.   if (NILP (arg))
  1098.     result = x_text_icon (f, 0);
  1099.   else
  1100.     result = x_bitmap_icon (f);
  1101.  
  1102.   if (result)
  1103.     {
  1104.       UNBLOCK_INPUT;
  1105.       error ("No icon window available.");
  1106.     }
  1107.  
  1108.   /* If the window was unmapped (and its icon was mapped),
  1109.      the new icon is not mapped, so map the window in its stead.  */
  1110.   if (FRAME_VISIBLE_P (f))
  1111. #ifdef USE_X_TOOLKIT
  1112.     XtPopup (f->display.x->widget, XtGrabNone);
  1113. #endif
  1114.     XMapWindow (XDISPLAY FRAME_X_WINDOW (f));
  1115.  
  1116.   XFlushQueue ();
  1117.   UNBLOCK_INPUT;
  1118. }
  1119.  
  1120. extern Lisp_Object x_new_font ();
  1121.  
  1122. void
  1123. x_set_font (f, arg, oldval)
  1124.      struct frame *f;
  1125.      Lisp_Object arg, oldval;
  1126. {
  1127.   Lisp_Object result;
  1128.  
  1129.   CHECK_STRING (arg, 1);
  1130.  
  1131.   BLOCK_INPUT;
  1132.   result = x_new_font (f, XSTRING (arg)->data);
  1133.   UNBLOCK_INPUT;
  1134.   
  1135.   if (EQ (result, Qnil))
  1136.     error ("Font \"%s\" is not defined", XSTRING (arg)->data);
  1137.   else if (EQ (result, Qt))
  1138.     error ("the characters of the given font have varying widths");
  1139.   else if (STRINGP (result))
  1140.     {
  1141.       recompute_basic_faces (f);
  1142.       store_frame_param (f, Qfont, result);
  1143.     }
  1144.   else
  1145.     abort ();
  1146. }
  1147.  
  1148. void
  1149. x_set_border_width (f, arg, oldval)
  1150.      struct frame *f;
  1151.      Lisp_Object arg, oldval;
  1152. {
  1153.   CHECK_NUMBER (arg, 0);
  1154.  
  1155.   if (XINT (arg) == f->display.x->border_width)
  1156.     return;
  1157.  
  1158.   if (FRAME_X_WINDOW (f) != 0)
  1159.     error ("Cannot change the border width of a window");
  1160.  
  1161.   f->display.x->border_width = XINT (arg);
  1162. }
  1163.  
  1164. void
  1165. x_set_internal_border_width (f, arg, oldval)
  1166.      struct frame *f;
  1167.      Lisp_Object arg, oldval;
  1168. {
  1169.   int mask;
  1170.   int old = f->display.x->internal_border_width;
  1171.  
  1172.   CHECK_NUMBER (arg, 0);
  1173.   f->display.x->internal_border_width = XINT (arg);
  1174.   if (f->display.x->internal_border_width < 0)
  1175.     f->display.x->internal_border_width = 0;
  1176.  
  1177.   if (f->display.x->internal_border_width == old)
  1178.     return;
  1179.  
  1180.   if (FRAME_X_WINDOW (f) != 0)
  1181.     {
  1182.       BLOCK_INPUT;
  1183.       x_set_window_size (f, 0, f->width, f->height);
  1184. #if 0
  1185.       x_set_resize_hint (f);
  1186. #endif
  1187.       XFlushQueue ();
  1188.       UNBLOCK_INPUT;
  1189.       SET_FRAME_GARBAGED (f);
  1190.     }
  1191. }
  1192.  
  1193. void
  1194. x_set_visibility (f, value, oldval)
  1195.      struct frame *f;
  1196.      Lisp_Object value, oldval;
  1197. {
  1198.   Lisp_Object frame;
  1199.   XSET (frame, Lisp_Frame, f);
  1200.  
  1201.   if (NILP (value))
  1202.     Fmake_frame_invisible (frame, Qt);
  1203.   else if (EQ (value, Qicon))
  1204.     Ficonify_frame (frame);
  1205.   else
  1206.     Fmake_frame_visible (frame);
  1207. }
  1208.  
  1209. static void
  1210. x_set_menu_bar_lines_1 (window, n)
  1211.   Lisp_Object window;
  1212.   int n;
  1213. {
  1214.   struct window *w = XWINDOW (window);
  1215.  
  1216.   XFASTINT (w->top) += n;
  1217.   XFASTINT (w->height) -= n;
  1218.  
  1219.   /* Handle just the top child in a vertical split.  */
  1220.   if (!NILP (w->vchild))
  1221.     x_set_menu_bar_lines_1 (w->vchild, n);
  1222.  
  1223.   /* Adjust all children in a horizontal split.  */
  1224.   for (window = w->hchild; !NILP (window); window = w->next)
  1225.     {
  1226.       w = XWINDOW (window);
  1227.       x_set_menu_bar_lines_1 (window, n);
  1228.     }
  1229. }
  1230.  
  1231. void
  1232. x_set_menu_bar_lines (f, value, oldval)
  1233.      struct frame *f;
  1234.      Lisp_Object value, oldval;
  1235. {
  1236.   int nlines;
  1237.   int olines = FRAME_MENU_BAR_LINES (f);
  1238.  
  1239.   /* Right now, menu bars don't work properly in minibuf-only frames;
  1240.      most of the commands try to apply themselves to the minibuffer
  1241.      frame itslef, and get an error because you can't switch buffers
  1242.      in or split the minibuffer window.  */
  1243.   if (FRAME_MINIBUF_ONLY_P (f))
  1244.     return;
  1245.  
  1246.   if (XTYPE (value) == Lisp_Int)
  1247.     nlines = XINT (value);
  1248.   else
  1249.     nlines = 0;
  1250.  
  1251. #ifdef USE_X_TOOLKIT
  1252.   FRAME_MENU_BAR_LINES (f) = 0;
  1253.   if (nlines)
  1254.     FRAME_EXTERNAL_MENU_BAR (f) = 1;
  1255.   else
  1256.     {
  1257.       if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
  1258.     free_frame_menubar (f);
  1259.       FRAME_EXTERNAL_MENU_BAR (f) = 0;
  1260.       f->display.x->menubar_widget = 0;
  1261.     }
  1262. #else /* not USE_X_TOOLKIT */
  1263.   FRAME_MENU_BAR_LINES (f) = nlines;
  1264.   x_set_menu_bar_lines_1 (f->root_window, nlines - olines);
  1265. #endif /* not USE_X_TOOLKIT */
  1266. }
  1267.  
  1268. /* Change the name of frame F to NAME.  If NAME is nil, set F's name to
  1269.        x_id_name.
  1270.  
  1271.    If EXPLICIT is non-zero, that indicates that lisp code is setting the
  1272.        name; if NAME is a string, set F's name to NAME and set
  1273.        F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
  1274.  
  1275.    If EXPLICIT is zero, that indicates that Emacs redisplay code is
  1276.        suggesting a new name, which lisp code should override; if
  1277.        F->explicit_name is set, ignore the new name; otherwise, set it.  */
  1278.  
  1279. void
  1280. x_set_name (f, name, explicit)
  1281.      struct frame *f;
  1282.      Lisp_Object name;
  1283.      int explicit;
  1284. {
  1285.   /* Make sure that requests from lisp code override requests from 
  1286.      Emacs redisplay code.  */
  1287.   if (explicit)
  1288.     {
  1289.       /* If we're switching from explicit to implicit, we had better
  1290.      update the mode lines and thereby update the title.  */
  1291.       if (f->explicit_name && NILP (name))
  1292.     update_mode_lines = 1;
  1293.  
  1294.       f->explicit_name = ! NILP (name);
  1295.     }
  1296.   else if (f->explicit_name)
  1297.     return;
  1298.  
  1299.   /* If NAME is nil, set the name to the x_id_name.  */
  1300.   if (NILP (name))
  1301.     {
  1302.       /* Check for no change needed in this very common case
  1303.      before we do any consing.  */
  1304.       if (!strcmp (x_id_name, XSTRING (f->name)->data))
  1305.     return;
  1306.       name = build_string (x_id_name);
  1307.     }
  1308.   else
  1309.     CHECK_STRING (name, 0);
  1310.  
  1311.   /* Don't change the name if it's already NAME.  */
  1312.   if (! NILP (Fstring_equal (name, f->name)))
  1313.     return;
  1314.  
  1315.   if (FRAME_X_WINDOW (f))
  1316.     {
  1317.       BLOCK_INPUT;
  1318. #ifdef HAVE_X11R4
  1319.       {
  1320.     XTextProperty text;
  1321.     text.value = XSTRING (name)->data;
  1322.     text.encoding = XA_STRING;
  1323.     text.format = 8;
  1324.     text.nitems = XSTRING (name)->size;
  1325. #ifdef USE_X_TOOLKIT
  1326.     XSetWMName (x_current_display, XtWindow (f->display.x->widget), &text);
  1327.     XSetWMIconName (x_current_display, XtWindow (f->display.x->widget),
  1328.             &text);
  1329. #else /* not USE_X_TOOLKIT */
  1330.     XSetWMName (x_current_display, FRAME_X_WINDOW (f), &text);
  1331.     XSetWMIconName (x_current_display, FRAME_X_WINDOW (f), &text);
  1332. #endif /* not USE_X_TOOLKIT */
  1333.       }
  1334. #else /* not HAVE_X11R4 */
  1335.       XSetIconName (XDISPLAY FRAME_X_WINDOW (f),
  1336.             XSTRING (name)->data);
  1337.       XStoreName (XDISPLAY FRAME_X_WINDOW (f),
  1338.           XSTRING (name)->data);
  1339. #endif /* not HAVE_X11R4 */
  1340.       UNBLOCK_INPUT;
  1341.     }
  1342.  
  1343.   f->name = name;
  1344. }
  1345.  
  1346. /* This function should be called when the user's lisp code has
  1347.    specified a name for the frame; the name will override any set by the
  1348.    redisplay code.  */
  1349. void
  1350. x_explicitly_set_name (f, arg, oldval)
  1351.      FRAME_PTR f;
  1352.      Lisp_Object arg, oldval;
  1353. {
  1354.   x_set_name (f, arg, 1);
  1355. }
  1356.  
  1357. /* This function should be called by Emacs redisplay code to set the
  1358.    name; names set this way will never override names set by the user's
  1359.    lisp code.  */
  1360. void
  1361. x_implicitly_set_name (f, arg, oldval)
  1362.      FRAME_PTR f;
  1363.      Lisp_Object arg, oldval;
  1364. {
  1365.   x_set_name (f, arg, 0);
  1366. }
  1367.  
  1368. void
  1369. x_set_autoraise (f, arg, oldval)
  1370.      struct frame *f;
  1371.      Lisp_Object arg, oldval;
  1372. {
  1373.   f->auto_raise = !EQ (Qnil, arg);
  1374. }
  1375.  
  1376. void
  1377. x_set_autolower (f, arg, oldval)
  1378.      struct frame *f;
  1379.      Lisp_Object arg, oldval;
  1380. {
  1381.   f->auto_lower = !EQ (Qnil, arg);
  1382. }
  1383.  
  1384. void
  1385. x_set_vertical_scroll_bars (f, arg, oldval)
  1386.      struct frame *f;
  1387.      Lisp_Object arg, oldval;
  1388. {
  1389.   if (NILP (arg) != ! FRAME_HAS_VERTICAL_SCROLL_BARS (f))
  1390.     {
  1391.       FRAME_HAS_VERTICAL_SCROLL_BARS (f) = ! NILP (arg);
  1392.  
  1393.       /* We set this parameter before creating the X window for the
  1394.      frame, so we can get the geometry right from the start.
  1395.      However, if the window hasn't been created yet, we shouldn't
  1396.      call x_set_window_size.  */
  1397.       if (FRAME_X_WINDOW (f))
  1398.     x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
  1399.     }
  1400. }
  1401.  
  1402. /* Subroutines of creating an X frame.  */
  1403.  
  1404. #ifdef HAVE_X11
  1405.  
  1406. /* Make sure that Vx_resource_name is set to a reasonable value.  */
  1407. static void
  1408. validate_x_resource_name ()
  1409. {
  1410.   if (STRINGP (Vx_resource_name))
  1411.     {
  1412.       int len = XSTRING (Vx_resource_name)->size;
  1413.       unsigned char *p = XSTRING (Vx_resource_name)->data;
  1414.       int i;
  1415.  
  1416.       /* Allow only letters, digits, - and _,
  1417.      because those are all that X allows.  */
  1418.       for (i = 0; i < len; i++)
  1419.     {
  1420.       int c = p[i];
  1421.       if (! ((c >= 'a' && c <= 'z')
  1422.          || (c >= 'A' && c <= 'Z')
  1423.          || (c >= '0' && c <= '9')
  1424.          || c == '-' || c == '_'))
  1425.         goto fail;
  1426.     }
  1427.     }
  1428.   else
  1429.   fail:
  1430.     Vx_resource_name = make_string ("emacs", 5);
  1431. }
  1432.  
  1433.  
  1434. extern char *x_get_string_resource ();
  1435. extern XrmDatabase x_load_resources ();
  1436.  
  1437. DEFUN ("x-get-resource", Fx_get_resource, Sx_get_resource, 2, 4, 0,
  1438.   "Return the value of ATTRIBUTE, of class CLASS, from the X defaults database.\n\
  1439. This uses `NAME.ATTRIBUTE' as the key and `Emacs.INSTANCE' as the\n\
  1440. class, where INSTANCE is the name under which Emacs was invoked, or\n\
  1441. the name specified by the `-name' or `-rn' command-line arguments.\n\
  1442. \n\
  1443. The optional arguments COMPONENT and SUBCLASS add to the key and the\n\
  1444. class, respectively.  You must specify both of them or neither.\n\
  1445. If you specify them, the key is `NAME.COMPONENT.ATTRIBUTE'\n\
  1446. and the class is `Emacs.INSTANCE.SUBCLASS'.")
  1447.   (attribute, class, component, subclass)
  1448.      Lisp_Object attribute, class, component, subclass;
  1449. {
  1450.   register char *value;
  1451.   char *name_key;
  1452.   char *class_key;
  1453.   Lisp_Object resname;
  1454.  
  1455.   check_x ();
  1456.  
  1457.   CHECK_STRING (attribute, 0);
  1458.   CHECK_STRING (class, 0);
  1459.  
  1460.   if (!NILP (component))
  1461.     CHECK_STRING (component, 1);
  1462.   if (!NILP (subclass))
  1463.     CHECK_STRING (subclass, 2);
  1464.   if (NILP (component) != NILP (subclass))
  1465.     error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
  1466.  
  1467.   validate_x_resource_name ();
  1468.   resname = Vx_resource_name;
  1469.  
  1470.   if (NILP (component))
  1471.     {
  1472.       /* Allocate space for the components, the dots which separate them,
  1473.      and the final '\0'.  */
  1474.       name_key = (char *) alloca (XSTRING (resname)->size
  1475.                   + XSTRING (attribute)->size
  1476.                   + 2);
  1477.       class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
  1478.                    + XSTRING (class)->size
  1479.                    + 2);
  1480.  
  1481.       sprintf (name_key, "%s.%s",
  1482.            XSTRING (resname)->data,
  1483.            XSTRING (attribute)->data);
  1484.       sprintf (class_key, "%s.%s",
  1485.            EMACS_CLASS,
  1486.            XSTRING (class)->data);
  1487.     }
  1488.   else
  1489.     {
  1490.       name_key = (char *) alloca (XSTRING (resname)->size
  1491.                   + XSTRING (component)->size
  1492.                   + XSTRING (attribute)->size
  1493.                   + 3);
  1494.  
  1495.       class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
  1496.                    + XSTRING (class)->size
  1497.                    + XSTRING (subclass)->size
  1498.                    + 3);
  1499.  
  1500.       sprintf (name_key, "%s.%s.%s",
  1501.            XSTRING (resname)->data,
  1502.            XSTRING (component)->data,
  1503.            XSTRING (attribute)->data);
  1504.       sprintf (class_key, "%s.%s.%s",
  1505.            EMACS_CLASS,
  1506.            XSTRING (class)->data,
  1507.            XSTRING (subclass)->data);
  1508.     }
  1509.  
  1510.   value = x_get_string_resource (xrdb, name_key, class_key);
  1511.  
  1512.   if (value != (char *) 0)
  1513.     return build_string (value);
  1514.   else
  1515.     return Qnil;
  1516. }
  1517.  
  1518. /* Used when C code wants a resource value.  */
  1519.  
  1520. char *
  1521. x_get_resource_string (attribute, class)
  1522.      char *attribute, *class;
  1523. {
  1524.   register char *value;
  1525.   char *name_key;
  1526.   char *class_key;
  1527.  
  1528.   /* Allocate space for the components, the dots which separate them,
  1529.      and the final '\0'.  */
  1530.   name_key = (char *) alloca (XSTRING (Vinvocation_name)->size
  1531.                   + strlen (attribute) + 2);
  1532.   class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
  1533.                    + strlen (class) + 2);
  1534.  
  1535.   sprintf (name_key, "%s.%s",
  1536.        XSTRING (Vinvocation_name)->data,
  1537.        attribute);
  1538.   sprintf (class_key, "%s.%s", EMACS_CLASS, class);
  1539.  
  1540.   return x_get_string_resource (xrdb, name_key, class_key);
  1541. }
  1542.  
  1543. #else    /* X10 */
  1544.  
  1545. DEFUN ("x-get-default", Fx_get_default, Sx_get_default, 1, 1, 0,
  1546.   "Get X default ATTRIBUTE from the system, or nil if no default.\n\
  1547. Value is a string (when not nil) and ATTRIBUTE is also a string.\n\
  1548. The defaults are specified in the file `~/.Xdefaults'.")
  1549.   (arg)
  1550.      Lisp_Object arg;
  1551. {
  1552.   register unsigned char *value;
  1553.  
  1554.   CHECK_STRING (arg, 1);
  1555.  
  1556.   value = (unsigned char *) XGetDefault (XDISPLAY 
  1557.                      XSTRING (Vinvocation_name)->data,
  1558.                      XSTRING (arg)->data);
  1559.   if (value == 0)
  1560.     /* Try reversing last two args, in case this is the buggy version of X.  */
  1561.     value = (unsigned char *) XGetDefault (XDISPLAY
  1562.                        XSTRING (arg)->data,
  1563.                        XSTRING (Vinvocation_name)->data);
  1564.   if (value != 0)
  1565.     return build_string (value);
  1566.   else
  1567.     return (Qnil);
  1568. }
  1569.  
  1570. #define Fx_get_resource(attribute, class, component, subclass) \
  1571.   Fx_get_default (attribute)
  1572.  
  1573. #endif    /* X10 */
  1574.  
  1575. /* Types we might convert a resource string into.  */
  1576. enum resource_types
  1577.   {
  1578.     number, boolean, string, symbol
  1579.   };
  1580.  
  1581. /* Return the value of parameter PARAM.
  1582.  
  1583.    First search ALIST, then Vdefault_frame_alist, then the X defaults
  1584.    database, using ATTRIBUTE as the attribute name and CLASS as its class.
  1585.  
  1586.    Convert the resource to the type specified by desired_type.
  1587.  
  1588.    If no default is specified, return Qunbound.  If you call
  1589.    x_get_arg, make sure you deal with Qunbound in a reasonable way,
  1590.    and don't let it get stored in any lisp-visible variables!  */
  1591.  
  1592. static Lisp_Object
  1593. x_get_arg (alist, param, attribute, class, type)
  1594.      Lisp_Object alist, param;
  1595.      char *attribute;
  1596.      char *class;
  1597.      enum resource_types type;
  1598. {
  1599.   register Lisp_Object tem;
  1600.  
  1601.   tem = Fassq (param, alist);
  1602.   if (EQ (tem, Qnil))
  1603.     tem = Fassq (param, Vdefault_frame_alist);
  1604.   if (EQ (tem, Qnil))
  1605.     {
  1606.  
  1607.       if (attribute)
  1608.     {
  1609.       tem = Fx_get_resource (build_string (attribute),
  1610.                  build_string (class),
  1611.                  Qnil, Qnil);
  1612.  
  1613.       if (NILP (tem))
  1614.         return Qunbound;
  1615.  
  1616.       switch (type)
  1617.         {
  1618.         case number:
  1619.           return make_number (atoi (XSTRING (tem)->data));
  1620.  
  1621.         case boolean:
  1622.           tem = Fdowncase (tem);
  1623.           if (!strcmp (XSTRING (tem)->data, "on")
  1624.           || !strcmp (XSTRING (tem)->data, "true"))
  1625.         return Qt;
  1626.           else 
  1627.         return Qnil;
  1628.  
  1629.         case string:
  1630.           return tem;
  1631.  
  1632.         case symbol:
  1633.           /* As a special case, we map the values `true' and `on'
  1634.          to Qt, and `false' and `off' to Qnil.  */
  1635.           {
  1636.         Lisp_Object lower;
  1637.         lower = Fdowncase (tem);
  1638.         if (!strcmp (XSTRING (lower)->data, "on")
  1639.             || !strcmp (XSTRING (lower)->data, "true"))
  1640.           return Qt;
  1641.         else if (!strcmp (XSTRING (lower)->data, "off")
  1642.               || !strcmp (XSTRING (lower)->data, "false"))
  1643.           return Qnil;
  1644.         else
  1645.           return Fintern (tem, Qnil);
  1646.           }
  1647.  
  1648.         default:
  1649.           abort ();
  1650.         }
  1651.     }
  1652.       else
  1653.     return Qunbound;
  1654.     }
  1655.   return Fcdr (tem);
  1656. }
  1657.  
  1658. /* Record in frame F the specified or default value according to ALIST
  1659.    of the parameter named PARAM (a Lisp symbol).
  1660.    If no value is specified for PARAM, look for an X default for XPROP
  1661.    on the frame named NAME.
  1662.    If that is not found either, use the value DEFLT.  */
  1663.  
  1664. static Lisp_Object
  1665. x_default_parameter (f, alist, prop, deflt, xprop, xclass, type)
  1666.      struct frame *f;
  1667.      Lisp_Object alist;
  1668.      Lisp_Object prop;
  1669.      Lisp_Object deflt;
  1670.      char *xprop;
  1671.      char *xclass;
  1672.      enum resource_types type;
  1673. {
  1674.   Lisp_Object tem;
  1675.  
  1676.   tem = x_get_arg (alist, prop, xprop, xclass, type);
  1677.   if (EQ (tem, Qunbound))
  1678.     tem = deflt;
  1679.   x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
  1680.   return tem;
  1681. }
  1682.  
  1683. DEFUN ("x-parse-geometry", Fx_parse_geometry, Sx_parse_geometry, 1, 1, 0,
  1684.        "Parse an X-style geometry string STRING.\n\
  1685. Returns an alist of the form ((top . TOP), (left . LEFT) ... ).\n\
  1686. The properties returned may include `top', `left', `height', and `width'.\n\
  1687. The value of `left' or `top' may be an integer or `-'.\n\
  1688. `-' means \"minus zero\".")
  1689.      (string)
  1690.      Lisp_Object string;
  1691. {
  1692.   int geometry, x, y;
  1693.   unsigned int width, height;
  1694.   Lisp_Object result;
  1695.  
  1696.   CHECK_STRING (string, 0);
  1697.  
  1698.   geometry = XParseGeometry ((char *) XSTRING (string)->data,
  1699.                  &x, &y, &width, &height);
  1700.  
  1701. #if 0
  1702.   if (!!(geometry & XValue) != !!(geometry & YValue))
  1703.     error ("Must specify both x and y position, or neither");
  1704. #endif
  1705.  
  1706.   result = Qnil;
  1707.   if (geometry & XValue)
  1708.     {
  1709.       Lisp_Object element;
  1710.  
  1711.       if (x == 0 && (geometry & XNegative))
  1712.     element = Fcons (Qleft, Qminus);
  1713.       else
  1714.     element = Fcons (Qleft, make_number (x));
  1715.       result = Fcons (element, result);
  1716.     }
  1717.  
  1718.   if (geometry & YValue)
  1719.     {
  1720.       Lisp_Object element;
  1721.  
  1722.       if (y == 0 && (geometry & YNegative))
  1723.     element = Fcons (Qtop, Qminus);
  1724.       else
  1725.     element = Fcons (Qtop, make_number (y));
  1726.       result = Fcons (element, result);
  1727.     }
  1728.  
  1729.   if (geometry & WidthValue)
  1730.     result = Fcons (Fcons (Qwidth, make_number (width)), result);
  1731.   if (geometry & HeightValue)
  1732.     result = Fcons (Fcons (Qheight, make_number (height)), result);
  1733.  
  1734.   return result;
  1735. }
  1736.  
  1737. #ifdef HAVE_X11
  1738. /* Calculate the desired size and position of this window,
  1739.    and return the flags saying which aspects were specified.
  1740.  
  1741.    This function does not make the coordinates positive.  */
  1742.  
  1743. #define DEFAULT_ROWS 40
  1744. #define DEFAULT_COLS 80
  1745.  
  1746. static int
  1747. x_figure_window_size (f, parms)
  1748.      struct frame *f;
  1749.      Lisp_Object parms;
  1750. {
  1751.   register Lisp_Object tem0, tem1, tem2;
  1752.   int height, width, left, top;
  1753.   register int geometry;
  1754.   long window_prompting = 0;
  1755.  
  1756.   /* Default values if we fall through.
  1757.      Actually, if that happens we should get
  1758.      window manager prompting. */
  1759.   f->width = DEFAULT_COLS;
  1760.   f->height = DEFAULT_ROWS;
  1761.   /* Window managers expect that if program-specified
  1762.      positions are not (0,0), they're intentional, not defaults.  */
  1763.   f->display.x->top_pos = 0;
  1764.   f->display.x->left_pos = 0;
  1765.  
  1766.   tem0 = x_get_arg (parms, Qheight, 0, 0, number);
  1767.   tem1 = x_get_arg (parms, Qwidth, 0, 0, number);
  1768.   tem2 = x_get_arg (parms, Quser_size, 0, 0, number);
  1769.   if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
  1770.     {
  1771.       if (!EQ (tem0, Qunbound))
  1772.     {
  1773.       CHECK_NUMBER (tem0, 0);
  1774.       f->height = XINT (tem0);
  1775.     }
  1776.       if (!EQ (tem1, Qunbound))
  1777.     {
  1778.       CHECK_NUMBER (tem1, 0);
  1779.       f->width = XINT (tem1);
  1780.     }
  1781.       if (!NILP (tem2) && !EQ (tem2, Qunbound))
  1782.     window_prompting |= USSize;
  1783.       else
  1784.     window_prompting |= PSize;
  1785.     }
  1786.  
  1787.   f->display.x->vertical_scroll_bar_extra
  1788.     = (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
  1789.        ? VERTICAL_SCROLL_BAR_PIXEL_WIDTH (f)
  1790.        : 0);
  1791.   f->display.x->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
  1792.   f->display.x->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
  1793.  
  1794.   tem0 = x_get_arg (parms, Qtop, 0, 0, number);
  1795.   tem1 = x_get_arg (parms, Qleft, 0, 0, number);
  1796.   tem2 = x_get_arg (parms, Quser_position, 0, 0, number);
  1797.   if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
  1798.     {
  1799.       if (EQ (tem0, Qminus))
  1800.     {
  1801.       f->display.x->top_pos = 0;
  1802.       window_prompting |= YNegative;
  1803.     }
  1804.       else if (EQ (tem0, Qunbound))
  1805.     f->display.x->top_pos = 0;
  1806.       else
  1807.     {
  1808.       CHECK_NUMBER (tem0, 0);
  1809.       f->display.x->top_pos = XINT (tem0);
  1810.       if (f->display.x->top_pos < 0)
  1811.         window_prompting |= YNegative;
  1812.     }
  1813.  
  1814.       if (EQ (tem1, Qminus))
  1815.     {
  1816.       f->display.x->left_pos = 0;
  1817.       window_prompting |= XNegative;
  1818.     }
  1819.       else if (EQ (tem1, Qunbound))
  1820.     f->display.x->left_pos = 0;
  1821.       else
  1822.     {
  1823.       CHECK_NUMBER (tem1, 0);
  1824.       f->display.x->left_pos = XINT (tem1);
  1825.       if (f->display.x->left_pos < 0)
  1826.         window_prompting |= XNegative;
  1827.     }
  1828.  
  1829.       if (!NILP (tem2))
  1830.     window_prompting |= USPosition;
  1831.       else
  1832.     window_prompting |= PPosition;
  1833.     }
  1834.  
  1835.   return window_prompting;
  1836. }
  1837.  
  1838. #if !defined (HAVE_X11R4) && !defined (HAVE_XSETWMPROTOCOLS)
  1839.  
  1840. Status
  1841. XSetWMProtocols (dpy, w, protocols, count)
  1842.      Display *dpy;
  1843.      Window w;
  1844.      Atom *protocols;
  1845.      int count;
  1846. {
  1847.   Atom prop;
  1848.   prop = XInternAtom (dpy, "WM_PROTOCOLS", False);
  1849.   if (prop == None) return False;
  1850.   XChangeProperty (dpy, w, prop, XA_ATOM, 32, PropModeReplace,
  1851.            (unsigned char *) protocols, count);
  1852.   return True;
  1853. }
  1854. #endif /* not HAVE_X11R4 && not HAVE_XSETWMPROTOCOLS */
  1855.  
  1856. #ifdef USE_X_TOOLKIT
  1857.  
  1858. /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
  1859.    WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them.  (They may
  1860.    already be present because of the toolkit (Motif adds some of them,
  1861.    for example, but Xt doesn't).  */
  1862.  
  1863. static void
  1864. hack_wm_protocols (widget)
  1865.      Widget widget;
  1866. {
  1867.   Display *dpy = XtDisplay (widget);
  1868.   Window w = XtWindow (widget);
  1869.   int need_delete = 1;
  1870.   int need_focus = 1;
  1871.   int need_save = 1;
  1872.  
  1873.   BLOCK_INPUT;
  1874.   {
  1875.     Atom type, *atoms = 0;
  1876.     int format = 0;
  1877.     unsigned long nitems = 0;
  1878.     unsigned long bytes_after;
  1879.  
  1880.     if (Success == XGetWindowProperty (dpy, w, Xatom_wm_protocols,
  1881.                        0, 100, False, XA_ATOM,
  1882.                        &type, &format, &nitems, &bytes_after,
  1883.                        (unsigned char **) &atoms)
  1884.     && format == 32 && type == XA_ATOM)
  1885.       while (nitems > 0)
  1886.     {
  1887.       nitems--;
  1888.        if (atoms[nitems] == Xatom_wm_delete_window)      need_delete = 0;
  1889.       else if (atoms[nitems] == Xatom_wm_take_focus)    need_focus = 0;
  1890.       else if (atoms[nitems] == Xatom_wm_save_yourself) need_save = 0;
  1891.     }
  1892.     if (atoms) XFree ((char *) atoms);
  1893.   }
  1894.   {
  1895.     Atom props [10];
  1896.     int count = 0;
  1897.     if (need_delete) props[count++] = Xatom_wm_delete_window;
  1898.     if (need_focus)  props[count++] = Xatom_wm_take_focus;
  1899.     if (need_save)   props[count++] = Xatom_wm_save_yourself;
  1900.     if (count)
  1901.       XChangeProperty (dpy, w, Xatom_wm_protocols, XA_ATOM, 32, PropModeAppend,
  1902.                (unsigned char *) props, count);
  1903.   }
  1904.   UNBLOCK_INPUT;
  1905. }
  1906. #endif
  1907.  
  1908. #ifdef USE_X_TOOLKIT
  1909.  
  1910. /* Create and set up the X widget for frame F.  */
  1911.  
  1912. static void
  1913. x_window (f, window_prompting, minibuffer_only)
  1914.      struct frame *f;
  1915.      long window_prompting;
  1916.      int minibuffer_only;
  1917. {
  1918.   XClassHint class_hints;
  1919.   XSetWindowAttributes attributes;
  1920.   unsigned long attribute_mask;
  1921.  
  1922.   Widget shell_widget;
  1923.   Widget pane_widget;
  1924.   Widget screen_widget;
  1925.   char* name;
  1926.   Arg al [25];
  1927.   int ac;
  1928.  
  1929.   BLOCK_INPUT;
  1930.  
  1931.   if (STRINGP (f->name))
  1932.     name = (char*) XSTRING (f->name)->data;
  1933.   else
  1934.     name = "emacs";
  1935.  
  1936.   ac = 0;
  1937.   XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
  1938.   XtSetArg (al[ac], XtNinput, 1); ac++;
  1939.   shell_widget = XtCreatePopupShell ("shell",
  1940.                      topLevelShellWidgetClass,
  1941.                      Xt_app_shell, al, ac);
  1942.  
  1943.   f->display.x->widget = shell_widget;
  1944.   /* maybe_set_screen_title_format (shell_widget); */
  1945.  
  1946.  
  1947.   ac = 0;
  1948.   XtSetArg (al[ac], XtNborderWidth, 0); ac++;
  1949.   pane_widget = XtCreateWidget ("pane",
  1950.                 panedWidgetClass,
  1951.                 shell_widget, al, ac);
  1952.  
  1953.   f->display.x->column_widget = pane_widget;
  1954.  
  1955.   if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
  1956.     initialize_frame_menubar (f);
  1957.  
  1958.   /* mappedWhenManaged to false tells to the paned window to not map/unmap 
  1959.      the emacs screen when changing menubar.  This reduces flickering.  */
  1960.  
  1961.   ac = 0;
  1962.   XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
  1963.   XtSetArg (al[ac], XtNshowGrip, 0); ac++;
  1964.   XtSetArg (al[ac], XtNallowResize, 1); ac++;
  1965.   XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
  1966.   XtSetArg (al[ac], XtNemacsFrame, f); ac++;
  1967.   screen_widget = XtCreateWidget (name,
  1968.                   emacsFrameClass,
  1969.                   pane_widget, al, ac);
  1970.  
  1971.   f->display.x->edit_widget = screen_widget;
  1972.  
  1973.   if (f->display.x->menubar_widget)
  1974.     XtManageChild (f->display.x->menubar_widget);
  1975.   XtManageChild (screen_widget); 
  1976.  
  1977.   /* Do some needed geometry management.  */
  1978.   {
  1979.     int len;
  1980.     char *tem, shell_position[32];
  1981.     Arg al[2];
  1982.     int ac = 0;
  1983.     int menubar_size 
  1984.       = (f->display.x->menubar_widget
  1985.      ? (f->display.x->menubar_widget->core.height
  1986.         + f->display.x->menubar_widget->core.border_width)
  1987.      : 0);
  1988.  
  1989.     if (FRAME_EXTERNAL_MENU_BAR (f))
  1990.       {
  1991.         Dimension ibw;
  1992.         XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
  1993.         menubar_size += ibw;
  1994.       }
  1995.  
  1996.     if (window_prompting & USPosition)
  1997.       {
  1998.     int left = f->display.x->left_pos;
  1999.     int xneg = window_prompting & XNegative;
  2000.     int top = f->display.x->top_pos;
  2001.     int yneg = window_prompting & YNegative;
  2002.     if (left < 0)
  2003.       left = -left;
  2004.     if (top < 0)
  2005.       top = -top;
  2006.     sprintf (shell_position, "=%dx%d%c%d%c%d", PIXEL_WIDTH (f), 
  2007.          PIXEL_HEIGHT (f) + menubar_size,
  2008.          (xneg ? '-' : '+'), left,
  2009.          (yneg ? '-' : '+'), top);
  2010.       }
  2011.     else
  2012.       sprintf (shell_position, "=%dx%d", PIXEL_WIDTH (f), 
  2013.            PIXEL_HEIGHT (f) + menubar_size);
  2014.     len = strlen (shell_position) + 1;
  2015.     tem = (char *) xmalloc (len);
  2016.     strncpy (tem, shell_position, len);
  2017.     XtSetArg (al[ac], XtNgeometry, tem); ac++;
  2018.     XtSetValues (shell_widget, al, ac);
  2019.   }
  2020.  
  2021.   x_calc_absolute_position (f);
  2022.  
  2023.   XtManageChild (pane_widget);
  2024.   XtRealizeWidget (shell_widget);
  2025.  
  2026.   FRAME_X_WINDOW (f) = XtWindow (screen_widget); 
  2027.  
  2028.   validate_x_resource_name ();
  2029.   class_hints.res_name = (char *) XSTRING (Vx_resource_name)->data;
  2030.   class_hints.res_class = EMACS_CLASS;
  2031.   XSetClassHint (x_current_display, XtWindow (shell_widget), &class_hints);
  2032.  
  2033.   f->display.x->wm_hints.input = True;
  2034.   f->display.x->wm_hints.flags |= InputHint;
  2035.   XSetWMHints (x_current_display, FRAME_X_WINDOW (f), &f->display.x->wm_hints);
  2036.  
  2037.   hack_wm_protocols (shell_widget);
  2038.  
  2039.   /* Do a stupid property change to force the server to generate a
  2040.      propertyNotify event so that the event_stream server timestamp will
  2041.      be initialized to something relevant to the time we created the window.
  2042.      */
  2043.   XChangeProperty (XtDisplay (screen_widget), XtWindow (screen_widget),
  2044.            Xatom_wm_protocols, XA_ATOM, 32, PropModeAppend,
  2045.            (unsigned char*) NULL, 0);
  2046.  
  2047.  /* Make all the standard events reach the Emacs frame.  */
  2048.   attributes.event_mask = STANDARD_EVENT_SET;
  2049.   attribute_mask = CWEventMask;
  2050.   XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
  2051.                attribute_mask, &attributes);
  2052.  
  2053.   XtMapWidget (screen_widget);
  2054.  
  2055.   /* x_set_name normally ignores requests to set the name if the
  2056.      requested name is the same as the current name.  This is the one
  2057.      place where that assumption isn't correct; f->name is set, but
  2058.      the X server hasn't been told.  */
  2059.   {
  2060.     Lisp_Object name;
  2061.     int explicit = f->explicit_name;
  2062.  
  2063.     f->explicit_name = 0;
  2064.     name = f->name;
  2065.     f->name = Qnil;
  2066.     x_set_name (f, name, explicit);
  2067.   }
  2068.  
  2069.   XDefineCursor (XDISPLAY FRAME_X_WINDOW (f),
  2070.          f->display.x->text_cursor);
  2071.  
  2072.   UNBLOCK_INPUT;
  2073.  
  2074.   if (FRAME_X_WINDOW (f) == 0)
  2075.     error ("Unable to create window");
  2076. }
  2077.  
  2078. #else /* not USE_X_TOOLKIT */
  2079.  
  2080. /* Create and set up the X window for frame F.  */
  2081.  
  2082. x_window (f)
  2083.      struct frame *f;
  2084.  
  2085. {
  2086.   XClassHint class_hints;
  2087.   XSetWindowAttributes attributes;
  2088.   unsigned long attribute_mask;
  2089.  
  2090.   attributes.background_pixel = f->display.x->background_pixel;
  2091.   attributes.border_pixel = f->display.x->border_pixel;
  2092.   attributes.bit_gravity = StaticGravity;
  2093.   attributes.backing_store = NotUseful;
  2094.   attributes.save_under = True;
  2095.   attributes.event_mask = STANDARD_EVENT_SET;
  2096.   attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity
  2097. #if 0
  2098.             | CWBackingStore | CWSaveUnder
  2099. #endif
  2100.             | CWEventMask);
  2101.  
  2102.   BLOCK_INPUT;
  2103.   FRAME_X_WINDOW (f)
  2104.     = XCreateWindow (x_current_display, ROOT_WINDOW,
  2105.              f->display.x->left_pos,
  2106.              f->display.x->top_pos,
  2107.              PIXEL_WIDTH (f), PIXEL_HEIGHT (f),
  2108.              f->display.x->border_width,
  2109.              CopyFromParent, /* depth */
  2110.              InputOutput, /* class */
  2111.              screen_visual, /* set in Fx_open_connection */
  2112.              attribute_mask, &attributes);
  2113.  
  2114.   validate_x_resource_name ();
  2115.   class_hints.res_name = (char *) XSTRING (Vx_resource_name)->data;
  2116.   class_hints.res_class = EMACS_CLASS;
  2117.   XSetClassHint (x_current_display, FRAME_X_WINDOW (f), &class_hints);
  2118.  
  2119.   /* This indicates that we use the "Passive Input" input model.
  2120.      Unless we do this, we don't get the Focus{In,Out} events that we
  2121.      need to draw the cursor correctly.  Accursed bureaucrats.
  2122.    XWhipsAndChains (x_current_display, IronMaiden, &TheRack);  */
  2123.  
  2124.   f->display.x->wm_hints.input = True;
  2125.   f->display.x->wm_hints.flags |= InputHint;
  2126.   XSetWMHints (x_current_display, FRAME_X_WINDOW (f), &f->display.x->wm_hints);
  2127.  
  2128.   /* Request "save yourself" and "delete window" commands from wm.  */
  2129.   {
  2130.     Atom protocols[2];
  2131.     protocols[0] = Xatom_wm_delete_window;
  2132.     protocols[1] = Xatom_wm_save_yourself;
  2133.     XSetWMProtocols (x_current_display, FRAME_X_WINDOW (f), protocols, 2);
  2134.   }
  2135.  
  2136.   /* x_set_name normally ignores requests to set the name if the
  2137.      requested name is the same as the current name.  This is the one
  2138.      place where that assumption isn't correct; f->name is set, but
  2139.      the X server hasn't been told.  */
  2140.   {
  2141.     Lisp_Object name;
  2142.     int explicit = f->explicit_name;
  2143.  
  2144.     f->explicit_name = 0;
  2145.     name = f->name;
  2146.     f->name = Qnil;
  2147.     x_set_name (f, name, explicit);
  2148.   }
  2149.  
  2150.   XDefineCursor (XDISPLAY FRAME_X_WINDOW (f),
  2151.          f->display.x->text_cursor);
  2152.  
  2153.   UNBLOCK_INPUT;
  2154.  
  2155.   if (FRAME_X_WINDOW (f) == 0)
  2156.     error ("Unable to create window");
  2157. }
  2158.  
  2159. #endif /* not USE_X_TOOLKIT */
  2160.  
  2161. /* Handle the icon stuff for this window.  Perhaps later we might
  2162.    want an x_set_icon_position which can be called interactively as
  2163.    well. */
  2164.  
  2165. static void
  2166. x_icon (f, parms)
  2167.      struct frame *f;
  2168.      Lisp_Object parms;
  2169. {
  2170.   Lisp_Object icon_x, icon_y;
  2171.  
  2172.   /* Set the position of the icon.  Note that twm groups all
  2173.      icons in an icon window. */
  2174.   icon_x = x_get_arg (parms, Qicon_left, 0, 0, number);
  2175.   icon_y = x_get_arg (parms, Qicon_top, 0, 0, number);
  2176.   if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
  2177.     {
  2178.       CHECK_NUMBER (icon_x, 0);
  2179.       CHECK_NUMBER (icon_y, 0);
  2180.     }
  2181.   else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
  2182.     error ("Both left and top icon corners of icon must be specified");
  2183.  
  2184.   BLOCK_INPUT;
  2185.  
  2186.   if (! EQ (icon_x, Qunbound))
  2187.     x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
  2188.  
  2189.   /* Start up iconic or window? */
  2190.   x_wm_set_window_state
  2191.     (f, (EQ (x_get_arg (parms, Qvisibility, 0, 0, symbol), Qicon)
  2192.      ? IconicState
  2193.      : NormalState));
  2194.  
  2195.   UNBLOCK_INPUT;
  2196. }
  2197.  
  2198. /* Make the GC's needed for this window, setting the
  2199.    background, border and mouse colors; also create the
  2200.    mouse cursor and the gray border tile.  */
  2201.  
  2202. static char cursor_bits[] =
  2203.   {
  2204.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2205.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2206.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2207.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  2208.   };
  2209.  
  2210. static void
  2211. x_make_gc (f)
  2212.      struct frame *f;
  2213. {
  2214.   XGCValues gc_values;
  2215.   GC temp_gc;
  2216.   XImage tileimage;
  2217.  
  2218.   BLOCK_INPUT;
  2219.  
  2220.   /* Create the GC's of this frame.
  2221.      Note that many default values are used.  */
  2222.  
  2223.   /* Normal video */
  2224.   gc_values.font = f->display.x->font->fid;
  2225.   gc_values.foreground = f->display.x->foreground_pixel;
  2226.   gc_values.background = f->display.x->background_pixel;
  2227.   gc_values.line_width = 0;    /* Means 1 using fast algorithm.  */
  2228.   f->display.x->normal_gc = XCreateGC (x_current_display,
  2229.                        FRAME_X_WINDOW (f),
  2230.                        GCLineWidth | GCFont
  2231.                        | GCForeground | GCBackground,
  2232.                        &gc_values);
  2233.  
  2234.   /* Reverse video style. */
  2235.   gc_values.foreground = f->display.x->background_pixel;
  2236.   gc_values.background = f->display.x->foreground_pixel;
  2237.   f->display.x->reverse_gc = XCreateGC (x_current_display,
  2238.                     FRAME_X_WINDOW (f),
  2239.                     GCFont | GCForeground | GCBackground
  2240.                     | GCLineWidth,
  2241.                     &gc_values);
  2242.  
  2243.   /* Cursor has cursor-color background, background-color foreground.  */
  2244.   gc_values.foreground = f->display.x->background_pixel;
  2245.   gc_values.background = f->display.x->cursor_pixel;
  2246.   gc_values.fill_style = FillOpaqueStippled;
  2247.   gc_values.stipple
  2248.     = XCreateBitmapFromData (x_current_display, ROOT_WINDOW,
  2249.                  cursor_bits, 16, 16);
  2250.   f->display.x->cursor_gc
  2251.     = XCreateGC (x_current_display, FRAME_X_WINDOW (f),
  2252.          (GCFont | GCForeground | GCBackground
  2253.           | GCFillStyle | GCStipple | GCLineWidth),
  2254.          &gc_values);
  2255.  
  2256.   /* Create the gray border tile used when the pointer is not in
  2257.      the frame.  Since this depends on the frame's pixel values,
  2258.      this must be done on a per-frame basis.  */
  2259.   f->display.x->border_tile
  2260.     = (XCreatePixmapFromBitmapData
  2261.        (x_current_display, ROOT_WINDOW, 
  2262.     gray_bits, gray_width, gray_height,
  2263.     f->display.x->foreground_pixel,
  2264.     f->display.x->background_pixel,
  2265.     DefaultDepth (x_current_display, XDefaultScreen (x_current_display))));
  2266.  
  2267.   UNBLOCK_INPUT;
  2268. }
  2269. #endif /* HAVE_X11 */
  2270.  
  2271. DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
  2272.        1, 1, 0,
  2273.   "Make a new X window, which is called a \"frame\" in Emacs terms.\n\
  2274. Return an Emacs frame object representing the X window.\n\
  2275. ALIST is an alist of frame parameters.\n\
  2276. If the parameters specify that the frame should not have a minibuffer,\n\
  2277. and do not specify a specific minibuffer window to use,\n\
  2278. then `default-minibuffer-frame' must be a frame whose minibuffer can\n\
  2279. be shared by the new frame.")
  2280.   (parms)
  2281.      Lisp_Object parms;
  2282. {
  2283. #ifdef HAVE_X11
  2284.   struct frame *f;
  2285.   Lisp_Object frame, tem;
  2286.   Lisp_Object name;
  2287.   int minibuffer_only = 0;
  2288.   long window_prompting = 0;
  2289.   int width, height;
  2290.   int count = specpdl_ptr - specpdl;
  2291.   struct gcpro gcpro1;
  2292.  
  2293.   check_x ();
  2294.  
  2295.   name = x_get_arg (parms, Qname, "title", "Title", string);
  2296.   if (XTYPE (name) != Lisp_String
  2297.       && ! EQ (name, Qunbound)
  2298.       && ! NILP (name))
  2299.     error ("x-create-frame: name parameter must be a string");
  2300.  
  2301.   tem = x_get_arg (parms, Qminibuffer, 0, 0, symbol);
  2302.   if (EQ (tem, Qnone) || NILP (tem))
  2303.     f = make_frame_without_minibuffer (Qnil);
  2304.   else if (EQ (tem, Qonly))
  2305.     {
  2306.       f = make_minibuffer_frame ();
  2307.       minibuffer_only = 1;
  2308.     }
  2309.   else if (XTYPE (tem) == Lisp_Window)
  2310.     f = make_frame_without_minibuffer (tem);
  2311.   else
  2312.     f = make_frame (1);
  2313.  
  2314.   /* Note that X Windows does support scroll bars.  */
  2315.   FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
  2316.  
  2317.   /* Set the name; the functions to which we pass f expect the name to
  2318.      be set.  */
  2319.   if (EQ (name, Qunbound) || NILP (name))
  2320.     {
  2321.       f->name = build_string (x_id_name);
  2322.       f->explicit_name = 0;
  2323.     }
  2324.   else
  2325.     {
  2326.       f->name = name;
  2327.       f->explicit_name = 1;
  2328.       /* use the frame's title when getting resources for this frame.  */
  2329.       specbind (Qx_resource_name, name);
  2330.     }
  2331.  
  2332.   XSET (frame, Lisp_Frame, f);
  2333.   GCPRO1 (frame);
  2334.  
  2335.   f->output_method = output_x_window;
  2336.   f->display.x = (struct x_display *) xmalloc (sizeof (struct x_display));
  2337.   bzero (f->display.x, sizeof (struct x_display));
  2338.  
  2339.   /* Note that the frame has no physical cursor right now.  */
  2340.   f->phys_cursor_x = -1;
  2341.  
  2342.   /* Extract the window parameters from the supplied values
  2343.      that are needed to determine window geometry.  */
  2344.   {
  2345.     Lisp_Object font;
  2346.  
  2347.     font = x_get_arg (parms, Qfont, "font", "Font", string);
  2348.     BLOCK_INPUT;
  2349.     /* First, try whatever font the caller has specified.  */
  2350.     if (STRINGP (font))
  2351.       font = x_new_font (f, XSTRING (font)->data);
  2352.     /* Try out a font which we hope has bold and italic variations.  */
  2353.     if (!STRINGP (font))
  2354.       font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
  2355.     if (! STRINGP (font))
  2356.       font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
  2357.     if (! STRINGP (font))
  2358.       /* This was formerly the first thing tried, but it finds too many fonts
  2359.      and takes too long.  */
  2360.       font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
  2361.     /* If those didn't work, look for something which will at least work.  */
  2362.     if (! STRINGP (font))
  2363.       font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
  2364.     UNBLOCK_INPUT;
  2365.     if (! STRINGP (font))
  2366.       font = build_string ("fixed");
  2367.  
  2368.     x_default_parameter (f, parms, Qfont, font, 
  2369.              "font", "Font", string);
  2370.   }
  2371.  
  2372.   x_default_parameter (f, parms, Qborder_width, make_number (2),
  2373.                "borderwidth", "BorderWidth", number);
  2374.   /* This defaults to 2 in order to match xterm.  We recognize either
  2375.      internalBorderWidth or internalBorder (which is what xterm calls
  2376.      it).  */
  2377.   if (NILP (Fassq (Qinternal_border_width, parms)))
  2378.     {
  2379.       Lisp_Object value;
  2380.  
  2381.       value = x_get_arg (parms, Qinternal_border_width,
  2382.              "internalBorder", "BorderWidth", number);
  2383.       if (! EQ (value, Qunbound))
  2384.     parms = Fcons (Fcons (Qinternal_border_width, value),
  2385.                parms);
  2386.     }
  2387.   x_default_parameter (f, parms, Qinternal_border_width, make_number (2),
  2388.                "internalBorderWidth", "BorderWidth", number);
  2389.   x_default_parameter (f, parms, Qvertical_scroll_bars, Qt,
  2390.                "verticalScrollBars", "ScrollBars", boolean);
  2391.  
  2392.   /* Also do the stuff which must be set before the window exists. */
  2393.   x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
  2394.                "foreground", "Foreground", string);
  2395.   x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
  2396.                "background", "Background", string);
  2397.   x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
  2398.                "pointerColor", "Foreground", string);
  2399.   x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
  2400.                "cursorColor", "Foreground", string);
  2401.   x_default_parameter (f, parms, Qborder_color, build_string ("black"),
  2402.                "borderColor", "BorderColor", string);
  2403.  
  2404.   x_default_parameter (f, parms, Qmenu_bar_lines, make_number (0),
  2405.                "menuBarLines", "MenuBarLines", number);
  2406.  
  2407.   f->display.x->parent_desc = ROOT_WINDOW;
  2408.   window_prompting = x_figure_window_size (f, parms);
  2409.  
  2410.   if (window_prompting & XNegative)
  2411.     {
  2412.       if (window_prompting & YNegative)
  2413.     f->display.x->win_gravity = SouthEastGravity;
  2414.       else
  2415.     f->display.x->win_gravity = NorthEastGravity;
  2416.     }
  2417.   else
  2418.     {
  2419.       if (window_prompting & YNegative)
  2420.     f->display.x->win_gravity = SouthWestGravity;
  2421.       else
  2422.     f->display.x->win_gravity = NorthWestGravity;
  2423.     }
  2424.  
  2425.   f->display.x->size_hint_flags = window_prompting;
  2426.  
  2427. #ifdef USE_X_TOOLKIT
  2428.   x_window (f, window_prompting, minibuffer_only);
  2429. #else
  2430.   x_window (f);
  2431. #endif
  2432.   x_icon (f, parms);
  2433.   x_make_gc (f);
  2434.   init_frame_faces (f);
  2435.  
  2436.   /* We need to do this after creating the X window, so that the
  2437.      icon-creation functions can say whose icon they're describing.  */
  2438.   x_default_parameter (f, parms, Qicon_type, Qnil,
  2439.                "bitmapIcon", "BitmapIcon", symbol);
  2440.  
  2441.   x_default_parameter (f, parms, Qauto_raise, Qnil,
  2442.                "autoRaise", "AutoRaiseLower", boolean);
  2443.   x_default_parameter (f, parms, Qauto_lower, Qnil,
  2444.                "autoLower", "AutoRaiseLower", boolean);
  2445.   x_default_parameter (f, parms, Qcursor_type, Qbox,
  2446.                "cursorType", "CursorType", symbol);
  2447.  
  2448.   /* Dimensions, especially f->height, must be done via change_frame_size.
  2449.      Change will not be effected unless different from the current
  2450.      f->height. */
  2451.   width = f->width;
  2452.   height = f->height;
  2453.   f->height = f->width = 0;
  2454.   change_frame_size (f, height, width, 1, 0);
  2455.  
  2456. /* With the toolkit, the geometry management is done in x_window.  */
  2457. #ifndef USE_X_TOOLKIT
  2458.   BLOCK_INPUT;
  2459.   x_wm_set_size_hint (f, window_prompting, 0);
  2460.   UNBLOCK_INPUT;
  2461. #endif /* USE_X_TOOLKIT */
  2462.  
  2463.   tem = x_get_arg (parms, Qunsplittable, 0, 0, boolean);
  2464.   f->no_split = minibuffer_only || EQ (tem, Qt);
  2465.  
  2466.   UNGCPRO;
  2467.  
  2468.   /* It is now ok to make the frame official
  2469.      even if we get an error below.
  2470.      And the frame needs to be on Vframe_list
  2471.      or making it visible won't work.  */
  2472.   Vframe_list = Fcons (frame, Vframe_list);
  2473.  
  2474.   /* Make the window appear on the frame and enable display,
  2475.      unless the caller says not to.  */
  2476.   {
  2477.     Lisp_Object visibility;
  2478.  
  2479.     visibility = x_get_arg (parms, Qvisibility, 0, 0, symbol);
  2480.     if (EQ (visibility, Qunbound))
  2481.       visibility = Qt;
  2482.  
  2483.     if (EQ (visibility, Qicon))
  2484.       x_iconify_frame (f);
  2485.     else if (! NILP (visibility))
  2486.       x_make_frame_visible (f);
  2487.     else
  2488.       /* Must have been Qnil.  */
  2489.       ;
  2490.   }
  2491.  
  2492.   return unbind_to (count, frame);
  2493. #else /* X10 */
  2494.   struct frame *f;
  2495.   Lisp_Object frame, tem;
  2496.   Lisp_Object name;
  2497.   int pixelwidth, pixelheight;
  2498.   Cursor cursor;
  2499.   int height, width;
  2500.   Window parent;
  2501.   Pixmap temp;
  2502.   int minibuffer_only = 0;
  2503.   Lisp_Object vscroll, hscroll;
  2504.  
  2505.   if (x_current_display == 0)
  2506.     error ("X windows are not in use or not initialized");
  2507.  
  2508.   name = Fassq (Qname, parms);
  2509.  
  2510.   tem = x_get_arg (parms, Qminibuffer, 0, 0, symbol);
  2511.   if (EQ (tem, Qnone))
  2512.     f = make_frame_without_minibuffer (Qnil);
  2513.   else if (EQ (tem, Qonly))
  2514.     {
  2515.       f = make_minibuffer_frame ();
  2516.       minibuffer_only = 1;
  2517.     }
  2518.   else if (EQ (tem, Qnil) || EQ (tem, Qunbound))
  2519.     f = make_frame (1);
  2520.   else
  2521.     f = make_frame_without_minibuffer (tem);
  2522.  
  2523.   parent = ROOT_WINDOW;
  2524.  
  2525.   XSET (frame, Lisp_Frame, f);
  2526.   f->output_method = output_x_window;
  2527.   f->display.x = (struct x_display *) xmalloc (sizeof (struct x_display));
  2528.   bzero (f->display.x, sizeof (struct x_display));
  2529.  
  2530.   /* Some temporary default values for height and width. */
  2531.   width = 80;
  2532.   height = 40;
  2533.   f->display.x->left_pos = -1;
  2534.   f->display.x->top_pos = -1;
  2535.  
  2536.   /* Give the frame a default name (which may be overridden with PARMS).  */
  2537.  
  2538.   strncpy (iconidentity, ICONTAG, MAXICID);
  2539.   if (gethostname (&iconidentity[sizeof (ICONTAG) - 1],
  2540.            (MAXICID - 1) - sizeof (ICONTAG)))
  2541.     iconidentity[sizeof (ICONTAG) - 2] = '\0';
  2542.   f->name = build_string (iconidentity);
  2543.  
  2544.   /* Extract some window parameters from the supplied values.
  2545.      These are the parameters that affect window geometry.  */
  2546.  
  2547.   tem = x_get_arg (parms, Qfont, "BodyFont", 0, string);
  2548.   if (EQ (tem, Qunbound))
  2549.     tem = build_string ("9x15");
  2550.   x_set_font (f, tem, Qnil);
  2551.   x_default_parameter (f, parms, Qborder_color,
  2552.                build_string ("black"), "Border", 0, string);
  2553.   x_default_parameter (f, parms, Qbackground_color,
  2554.                build_string ("white"), "Background", 0, string);
  2555.   x_default_parameter (f, parms, Qforeground_color,
  2556.                build_string ("black"), "Foreground", 0, string);
  2557.   x_default_parameter (f, parms, Qmouse_color,
  2558.                build_string ("black"), "Mouse", 0, string);
  2559.   x_default_parameter (f, parms, Qcursor_color,
  2560.                build_string ("black"), "Cursor", 0, string);
  2561.   x_default_parameter (f, parms, Qborder_width,
  2562.                make_number (2), "BorderWidth", 0, number);
  2563.   x_default_parameter (f, parms, Qinternal_border_width,
  2564.                make_number (4), "InternalBorderWidth", 0, number);
  2565.   x_default_parameter (f, parms, Qauto_raise,
  2566.                Qnil, "AutoRaise", 0, boolean);
  2567.  
  2568.   hscroll = EQ (x_get_arg (parms, Qhorizontal_scroll_bar, 0, 0, boolean), Qt);
  2569.   vscroll = EQ (x_get_arg (parms, Qvertical_scroll_bar, 0, 0, boolean), Qt);
  2570.  
  2571.   if (f->display.x->internal_border_width < 0)
  2572.     f->display.x->internal_border_width = 0;
  2573.  
  2574.   tem = x_get_arg (parms, Qwindow_id, 0, 0, number);
  2575.   if (!EQ (tem, Qunbound))
  2576.     {
  2577.       WINDOWINFO_TYPE wininfo;
  2578.       int nchildren;
  2579.       Window *children, root;
  2580.  
  2581.       CHECK_NUMBER (tem, 0);
  2582.       FRAME_X_WINDOW (f) = (Window) XINT (tem);
  2583.  
  2584.       BLOCK_INPUT;
  2585.       XGetWindowInfo (FRAME_X_WINDOW (f), &wininfo);
  2586.       XQueryTree (FRAME_X_WINDOW (f), &parent, &nchildren, &children);
  2587.       xfree (children);
  2588.       UNBLOCK_INPUT;
  2589.  
  2590.       height = PIXEL_TO_CHAR_HEIGHT (f, wininfo.height);
  2591.       width  = PIXEL_TO_CHAR_WIDTH  (f, wininfo.width);
  2592.       f->display.x->left_pos = wininfo.x;
  2593.       f->display.x->top_pos = wininfo.y;
  2594.       FRAME_SET_VISIBILITY (f, wininfo.mapped != 0);
  2595.       f->display.x->border_width = wininfo.bdrwidth;
  2596.       f->display.x->parent_desc = parent;
  2597.     }
  2598.   else
  2599.     {
  2600.       tem = x_get_arg (parms, Qparent_id, 0, 0, number);
  2601.       if (!EQ (tem, Qunbound))
  2602.     {
  2603.       CHECK_NUMBER (tem, 0);
  2604.       parent = (Window) XINT (tem);
  2605.     }
  2606.       f->display.x->parent_desc = parent;
  2607.       tem = x_get_arg (parms, Qheight, 0, 0, number);
  2608.       if (EQ (tem, Qunbound))
  2609.     {
  2610.       tem = x_get_arg (parms, Qwidth, 0, 0, number);
  2611.       if (EQ (tem, Qunbound))
  2612.         {
  2613.           tem = x_get_arg (parms, Qtop, 0, 0, number);
  2614.           if (EQ (tem, Qunbound))
  2615.         tem = x_get_arg (parms, Qleft, 0, 0, number);
  2616.         }
  2617.     }
  2618.       /* Now TEM is Qunbound if no edge or size was specified.
  2619.      In that case, we must do rubber-banding.  */
  2620.       if (EQ (tem, Qunbound))
  2621.     {
  2622.       tem = x_get_arg (parms, Qgeometry, 0, 0, number);
  2623.       x_rubber_band (f,
  2624.              &f->display.x->left_pos, &f->display.x->top_pos,
  2625.              &width, &height,
  2626.              (XTYPE (tem) == Lisp_String
  2627.               ? (char *) XSTRING (tem)->data : ""),
  2628.              XSTRING (f->name)->data,
  2629.              !NILP (hscroll), !NILP (vscroll));
  2630.     }
  2631.       else
  2632.     {
  2633.       /* Here if at least one edge or size was specified.
  2634.          Demand that they all were specified, and use them.  */
  2635.       tem = x_get_arg (parms, Qheight, 0, 0, number);
  2636.       if (EQ (tem, Qunbound))
  2637.         error ("Height not specified");
  2638.       CHECK_NUMBER (tem, 0);
  2639.       height = XINT (tem);
  2640.  
  2641.       tem = x_get_arg (parms, Qwidth, 0, 0, number);
  2642.       if (EQ (tem, Qunbound))
  2643.         error ("Width not specified");
  2644.       CHECK_NUMBER (tem, 0);
  2645.       width = XINT (tem);
  2646.  
  2647.       tem = x_get_arg (parms, Qtop, 0, 0, number);
  2648.       if (EQ (tem, Qunbound))
  2649.         error ("Top position not specified");
  2650.       CHECK_NUMBER (tem, 0);
  2651.       f->display.x->left_pos = XINT (tem);
  2652.  
  2653.       tem = x_get_arg (parms, Qleft, 0, 0, number);
  2654.       if (EQ (tem, Qunbound))
  2655.         error ("Left position not specified");
  2656.       CHECK_NUMBER (tem, 0);
  2657.       f->display.x->top_pos = XINT (tem);
  2658.     }
  2659.  
  2660.       pixelwidth  = CHAR_TO_PIXEL_WIDTH  (f, width);
  2661.       pixelheight = CHAR_TO_PIXEL_HEIGHT (f, height);
  2662.       
  2663.       BLOCK_INPUT;
  2664.       FRAME_X_WINDOW (f)
  2665.     = XCreateWindow (parent,
  2666.              f->display.x->left_pos,   /* Absolute horizontal offset */
  2667.              f->display.x->top_pos,    /* Absolute Vertical offset */
  2668.              pixelwidth, pixelheight,
  2669.              f->display.x->border_width,
  2670.              BLACK_PIX_DEFAULT, WHITE_PIX_DEFAULT);
  2671.       UNBLOCK_INPUT;
  2672.       if (FRAME_X_WINDOW (f) == 0)
  2673.     error ("Unable to create window.");
  2674.     }
  2675.  
  2676.   /* Install the now determined height and width
  2677.      in the windows and in phys_lines and desired_lines.  */
  2678.   change_frame_size (f, height, width, 1, 0);
  2679.   XSelectInput (FRAME_X_WINDOW (f), KeyPressed | ExposeWindow
  2680.         | ButtonPressed | ButtonReleased | ExposeRegion | ExposeCopy
  2681.         | EnterWindow | LeaveWindow | UnmapWindow );
  2682.   x_set_resize_hint (f);
  2683.  
  2684.   /* Tell the server the window's default name.  */
  2685.   XStoreName (XDISPLAY FRAME_X_WINDOW (f), XSTRING (f->name)->data);
  2686.  
  2687.   /* Now override the defaults with all the rest of the specified
  2688.      parms.  */
  2689.   tem = x_get_arg (parms, Qunsplittable, 0, 0, boolean);
  2690.   f->no_split = minibuffer_only || EQ (tem, Qt);
  2691.  
  2692.   /* Do not create an icon window if the caller says not to */
  2693.   if (!EQ (x_get_arg (parms, Qsuppress_icon, 0, 0, boolean), Qt)
  2694.       || f->display.x->parent_desc != ROOT_WINDOW)
  2695.     {
  2696.       x_text_icon (f, iconidentity);
  2697.       x_default_parameter (f, parms, Qicon_type, Qnil,
  2698.                "BitmapIcon", 0, symbol);
  2699.     }
  2700.  
  2701.   /* Tell the X server the previously set values of the
  2702.      background, border and mouse colors; also create the mouse cursor.  */
  2703.   BLOCK_INPUT;
  2704.   temp = XMakeTile (f->display.x->background_pixel);
  2705.   XChangeBackground (FRAME_X_WINDOW (f), temp);
  2706.   XFreePixmap (temp);
  2707.   UNBLOCK_INPUT;
  2708.   x_set_border_pixel (f, f->display.x->border_pixel);
  2709.  
  2710.   x_set_mouse_color (f, Qnil, Qnil);
  2711.  
  2712.   /* Now override the defaults with all the rest of the specified parms.  */
  2713.  
  2714.   Fmodify_frame_parameters (frame, parms);
  2715.  
  2716.   /* Make the window appear on the frame and enable display.  */
  2717.   {
  2718.     Lisp_Object visibility;
  2719.  
  2720.     visibility = x_get_arg (parms, Qvisibility, 0, 0, symbol);
  2721.     if (EQ (visibility, Qunbound))
  2722.       visibility = Qt;
  2723.  
  2724.     if (! EQ (visibility, Qicon)
  2725.     && ! NILP (visibility))
  2726.       x_make_window_visible (f);
  2727.   }
  2728.  
  2729.   SET_FRAME_GARBAGED (f);
  2730.  
  2731.   Vframe_list = Fcons (frame, Vframe_list);
  2732.   return frame;
  2733. #endif /* X10 */
  2734. }
  2735.  
  2736. Lisp_Object
  2737. x_get_focus_frame ()
  2738. {
  2739.   Lisp_Object xfocus;
  2740.   if (! x_focus_frame)
  2741.     return Qnil;
  2742.  
  2743.   XSET (xfocus, Lisp_Frame, x_focus_frame);
  2744.   return xfocus;
  2745. }
  2746.  
  2747. DEFUN ("focus-frame", Ffocus_frame, Sfocus_frame, 1, 1, 0,
  2748.   "Set the focus on FRAME.")
  2749.   (frame)
  2750.      Lisp_Object frame;
  2751. {
  2752.   CHECK_LIVE_FRAME (frame, 0);
  2753.  
  2754.   if (FRAME_X_P (XFRAME (frame)))
  2755.     {
  2756.       BLOCK_INPUT;
  2757.       x_focus_on_frame (XFRAME (frame));
  2758.       UNBLOCK_INPUT;
  2759.       return frame;
  2760.     }
  2761.  
  2762.   return Qnil;
  2763. }
  2764.  
  2765. DEFUN ("unfocus-frame", Funfocus_frame, Sunfocus_frame, 0, 0, 0,
  2766.   "If a frame has been focused, release it.")
  2767.   ()
  2768. {
  2769.   if (x_focus_frame)
  2770.     {
  2771.       BLOCK_INPUT;
  2772.       x_unfocus_frame (x_focus_frame);
  2773.       UNBLOCK_INPUT;
  2774.     }
  2775.  
  2776.   return Qnil;
  2777. }
  2778.  
  2779. #ifndef HAVE_X11
  2780. /* Computes an X-window size and position either from geometry GEO
  2781.    or with the mouse.
  2782.  
  2783.    F is a frame.  It specifies an X window which is used to
  2784.    determine which display to compute for.  Its font, borders
  2785.    and colors control how the rectangle will be displayed.
  2786.  
  2787.    X and Y are where to store the positions chosen.
  2788.    WIDTH and HEIGHT are where to store the sizes chosen.
  2789.  
  2790.    GEO is the geometry that may specify some of the info.
  2791.    STR is a prompt to display.
  2792.    HSCROLL and VSCROLL say whether we have horiz and vert scroll bars.  */
  2793.  
  2794. int
  2795. x_rubber_band (f, x, y, width, height, geo, str, hscroll, vscroll)
  2796.      struct frame *f;
  2797.      int *x, *y, *width, *height;
  2798.      char *geo;
  2799.      char *str;
  2800.      int hscroll, vscroll;
  2801. {
  2802.   OpaqueFrame frame;
  2803.   Window tempwindow;
  2804.   WindowInfo wininfo;
  2805.   int border_color;
  2806.   int background_color;
  2807.   Lisp_Object tem;
  2808.   int mask;
  2809.  
  2810.   BLOCK_INPUT;
  2811.  
  2812.   background_color = f->display.x->background_pixel;
  2813.   border_color = f->display.x->border_pixel;
  2814.  
  2815.   frame.bdrwidth = f->display.x->border_width;
  2816.   frame.border = XMakeTile (border_color);
  2817.   frame.background = XMakeTile (background_color);
  2818.   tempwindow = XCreateTerm (str, "emacs", geo, default_window, &frame, 10, 5,
  2819.                 (2 * f->display.x->internal_border_width
  2820.                  + (vscroll ? VSCROLL_WIDTH : 0)),
  2821.                 (2 * f->display.x->internal_border_width
  2822.                  + (hscroll ? HSCROLL_HEIGHT : 0)),
  2823.                 width, height, f->display.x->font,
  2824.                 FONT_WIDTH (f->display.x->font),
  2825.                 f->display.x->line_height);
  2826.   XFreePixmap (frame.border);
  2827.   XFreePixmap (frame.background);
  2828.  
  2829.   if (tempwindow != 0)
  2830.     {
  2831.       XQueryWindow (tempwindow, &wininfo);
  2832.       XDestroyWindow (tempwindow);
  2833.       *x = wininfo.x;
  2834.       *y = wininfo.y;
  2835.     }
  2836.  
  2837.   /* Coordinates we got are relative to the root window.
  2838.      Convert them to coordinates relative to desired parent window
  2839.      by scanning from there up to the root.  */
  2840.   tempwindow = f->display.x->parent_desc;
  2841.   while (tempwindow != ROOT_WINDOW)
  2842.     {
  2843.       int nchildren;
  2844.       Window *children;
  2845.       XQueryWindow (tempwindow, &wininfo);
  2846.       *x -= wininfo.x;
  2847.       *y -= wininfo.y;
  2848.       XQueryTree (tempwindow, &tempwindow, &nchildren, &children);
  2849.       xfree (children);
  2850.     }
  2851.  
  2852.   UNBLOCK_INPUT;
  2853.   return tempwindow != 0;
  2854. }
  2855. #endif /* not HAVE_X11 */
  2856.  
  2857. DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 3, 0,
  2858.   "Return a list of the names of available fonts matching PATTERN.\n\
  2859. If optional arguments FACE and FRAME are specified, return only fonts\n\
  2860. the same size as FACE on FRAME.\n\
  2861. \n\
  2862. PATTERN is a string, perhaps with wildcard characters;\n\
  2863.   the * character matches any substring, and\n\
  2864.   the ? character matches any single character.\n\
  2865.   PATTERN is case-insensitive.\n\
  2866. FACE is a face name - a symbol.\n\
  2867. \n\
  2868. The return value is a list of strings, suitable as arguments to\n\
  2869. set-face-font.\n\
  2870. \n\
  2871. Fonts Emacs can't use (i.e. proportional fonts) may or may not be excluded\n\
  2872. even if they match PATTERN and FACE.")
  2873.   (pattern, face, frame)
  2874.     Lisp_Object pattern, face, frame;
  2875. {
  2876.   int num_fonts;
  2877.   char **names;
  2878.   XFontStruct *info;
  2879.   XFontStruct *size_ref;
  2880.   Lisp_Object list;
  2881.  
  2882.   check_x ();
  2883.   CHECK_STRING (pattern, 0);
  2884.   if (!NILP (face))
  2885.     CHECK_SYMBOL (face, 1);
  2886.   if (!NILP (frame))
  2887.     CHECK_LIVE_FRAME (frame, 2);
  2888.  
  2889.   if (NILP (face))
  2890.     size_ref = 0;
  2891.   else
  2892.     {
  2893.       FRAME_PTR f = NILP (frame) ? selected_frame : XFRAME (frame);
  2894.       int face_id;
  2895.  
  2896.       /* Don't die if we get called with a terminal frame.  */
  2897.       if (! FRAME_X_P (f))
  2898.     error ("non-X frame used in `x-list-fonts'");
  2899.  
  2900.       face_id = face_name_id_number (f, face);
  2901.  
  2902.       if (face_id < 0 || face_id >= FRAME_N_PARAM_FACES (f)
  2903.       || FRAME_PARAM_FACES (f) [face_id] == 0)
  2904.     size_ref = f->display.x->font;
  2905.       else
  2906.     {
  2907.       size_ref = FRAME_PARAM_FACES (f) [face_id]->font;
  2908.       if (size_ref == (XFontStruct *) (~0))
  2909.         size_ref = f->display.x->font;
  2910.     }
  2911.     }
  2912.  
  2913.   BLOCK_INPUT;
  2914.  
  2915.   /* Solaris 2.3 has a bug in XListFontsWithInfo.  */
  2916. #ifdef BROKEN_XLISTFONTSWITHINFO
  2917.   names = XListFonts (x_current_display,
  2918.                       XSTRING (pattern)->data,
  2919.                       2000, /* maxnames */
  2920.                       &num_fonts); /* count_return */
  2921. #else
  2922.   names = XListFontsWithInfo (x_current_display,
  2923.                   XSTRING (pattern)->data,
  2924.                   2000, /* maxnames */
  2925.                   &num_fonts, /* count_return */
  2926.                   &info); /* info_return */
  2927. #endif
  2928.   UNBLOCK_INPUT;
  2929.  
  2930.   list = Qnil;
  2931.  
  2932.   if (names)
  2933.     {
  2934.       Lisp_Object *tail;
  2935.       int i;
  2936.  
  2937.       tail = &list;
  2938.       for (i = 0; i < num_fonts; i++)
  2939.         {
  2940.       XFontStruct *thisinfo;
  2941.  
  2942. #ifdef BROKEN_XLISTFONTSWITHINFO
  2943.           BLOCK_INPUT;
  2944.           thisinfo = XLoadQueryFont (x_current_display, names[i]);
  2945.           UNBLOCK_INPUT;
  2946. #else
  2947.       thisinfo = &info[i];
  2948. #endif
  2949.           if (thisinfo && (! size_ref
  2950.                || same_size_fonts (thisinfo, size_ref)))
  2951.         {
  2952.           *tail = Fcons (build_string (names[i]), Qnil);
  2953.           tail = &XCONS (*tail)->cdr;
  2954.         }
  2955.         }
  2956.  
  2957.       BLOCK_INPUT;
  2958. #ifdef BROKEN_XLISTFONTSWITHINFO
  2959.       XFreeFontNames (names);
  2960. #else
  2961.       XFreeFontInfo (names, info, num_fonts);
  2962. #endif
  2963.       UNBLOCK_INPUT;
  2964.     }
  2965.  
  2966.   return list;
  2967. }
  2968.  
  2969.  
  2970. DEFUN ("x-color-defined-p", Fx_color_defined_p, Sx_color_defined_p, 1, 1, 0,
  2971.   "Return non-nil if the X display supports the color named COLOR.")
  2972.   (color)
  2973.      Lisp_Object color;
  2974. {
  2975.   Color foo;
  2976.   
  2977.   check_x ();
  2978.   CHECK_STRING (color, 0);
  2979.  
  2980.   if (defined_color (XSTRING (color)->data, &foo, 0))
  2981.     return Qt;
  2982.   else
  2983.     return Qnil;
  2984. }
  2985.  
  2986. DEFUN ("x-color-values", Fx_color_values, Sx_color_values, 1, 1, 0,
  2987.   "Return a description of the color named COLOR.\n\
  2988. The value is a list of integer RGB values--(RED GREEN BLUE).\n\
  2989. These values appear to range from 0 to 65280; white is (65280 65280 65280).")
  2990.   (color)
  2991.      Lisp_Object color;
  2992. {
  2993.   Color foo;
  2994.   
  2995.   check_x ();
  2996.   CHECK_STRING (color, 0);
  2997.  
  2998.   if (defined_color (XSTRING (color)->data, &foo, 0))
  2999.     {
  3000.       Lisp_Object rgb[3];
  3001.  
  3002.       rgb[0] = make_number (foo.red);
  3003.       rgb[1] = make_number (foo.green);
  3004.       rgb[2] = make_number (foo.blue);
  3005.       return Flist (3, rgb);
  3006.     }
  3007.   else
  3008.     return Qnil;
  3009. }
  3010.  
  3011. DEFUN ("x-display-color-p", Fx_display_color_p, Sx_display_color_p, 0, 0, 0,
  3012.   "Return t if the X screen currently in use supports color.")
  3013.   ()
  3014. {
  3015.   check_x ();
  3016.  
  3017.   if (x_screen_planes <= 2)
  3018.     return Qnil;
  3019.  
  3020.   switch (screen_visual->class)
  3021.     {
  3022.     case StaticColor:
  3023.     case PseudoColor:
  3024.     case TrueColor:
  3025.     case DirectColor:
  3026.       return Qt;
  3027.  
  3028.     default:
  3029.       return Qnil;
  3030.     }
  3031. }
  3032.  
  3033. DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
  3034.   0, 1, 0,
  3035.   "Returns the width in pixels of the display FRAME is on.")
  3036.   (frame)
  3037.      Lisp_Object frame;
  3038. {
  3039.   Display *dpy = x_current_display;
  3040.   check_x ();
  3041.   return make_number (DisplayWidth (dpy, DefaultScreen (dpy)));
  3042. }
  3043.  
  3044. DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
  3045.   Sx_display_pixel_height, 0, 1, 0,
  3046.   "Returns the height in pixels of the display FRAME is on.")
  3047.   (frame)
  3048.      Lisp_Object frame;
  3049. {
  3050.   Display *dpy = x_current_display;
  3051.   check_x ();
  3052.   return make_number (DisplayHeight (dpy, DefaultScreen (dpy)));
  3053. }
  3054.  
  3055. DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
  3056.   0, 1, 0,
  3057.   "Returns the number of bitplanes of the display FRAME is on.")
  3058.   (frame)
  3059.      Lisp_Object frame;
  3060. {
  3061.   Display *dpy = x_current_display;
  3062.   check_x ();
  3063.   return make_number (DisplayPlanes (dpy, DefaultScreen (dpy)));
  3064. }
  3065.  
  3066. DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
  3067.   0, 1, 0,
  3068.   "Returns the number of color cells of the display FRAME is on.")
  3069.   (frame)
  3070.      Lisp_Object frame;
  3071. {
  3072.   Display *dpy = x_current_display;
  3073.   check_x ();
  3074.   return make_number (DisplayCells (dpy, DefaultScreen (dpy)));
  3075. }
  3076.  
  3077. DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
  3078.        Sx_server_max_request_size,
  3079.   0, 1, 0,
  3080.   "Returns the maximum request size of the X server FRAME is using.")
  3081.   (frame)
  3082.      Lisp_Object frame;
  3083. {
  3084.   Display *dpy = x_current_display;
  3085.   check_x ();
  3086.   return make_number (MAXREQUEST (dpy));
  3087. }
  3088.  
  3089. DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
  3090.   "Returns the vendor ID string of the X server FRAME is on.")
  3091.   (frame)
  3092.      Lisp_Object frame;
  3093. {
  3094.   Display *dpy = x_current_display;
  3095.   char *vendor;
  3096.   check_x ();
  3097.   vendor = ServerVendor (dpy);
  3098.   if (! vendor) vendor = "";
  3099.   return build_string (vendor);
  3100. }
  3101.  
  3102. DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
  3103.   "Returns the version numbers of the X server in use.\n\
  3104. The value is a list of three integers: the major and minor\n\
  3105. version numbers of the X Protocol in use, and the vendor-specific release\n\
  3106. number.  See also the variable `x-server-vendor'.")
  3107.   (frame)
  3108.      Lisp_Object frame;
  3109. {
  3110.   Display *dpy = x_current_display;
  3111.  
  3112.   check_x ();
  3113.   return Fcons (make_number (ProtocolVersion (dpy)),
  3114.         Fcons (make_number (ProtocolRevision (dpy)),
  3115.                Fcons (make_number (VendorRelease (dpy)), Qnil)));
  3116. }
  3117.  
  3118. DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
  3119.   "Returns the number of screens on the X server FRAME is on.")
  3120.   (frame)
  3121.      Lisp_Object frame;
  3122. {
  3123.   check_x ();
  3124.   return make_number (ScreenCount (x_current_display));
  3125. }
  3126.  
  3127. DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
  3128.   "Returns the height in millimeters of the X screen FRAME is on.")
  3129.   (frame)
  3130.      Lisp_Object frame;
  3131. {
  3132.   check_x ();
  3133.   return make_number (HeightMMOfScreen (x_screen));
  3134. }
  3135.  
  3136. DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
  3137.   "Returns the width in millimeters of the X screen FRAME is on.")
  3138.   (frame)
  3139.      Lisp_Object frame;
  3140. {
  3141.   check_x ();
  3142.   return make_number (WidthMMOfScreen (x_screen));
  3143. }
  3144.  
  3145. DEFUN ("x-display-backing-store", Fx_display_backing_store,
  3146.   Sx_display_backing_store, 0, 1, 0,
  3147.   "Returns an indication of whether the X screen FRAME is on does backing store.\n\
  3148. The value may be `always', `when-mapped', or `not-useful'.")
  3149.   (frame)
  3150.      Lisp_Object frame;
  3151. {
  3152.   check_x ();
  3153.  
  3154.   switch (DoesBackingStore (x_screen))
  3155.     {
  3156.     case Always:
  3157.       return intern ("always");
  3158.  
  3159.     case WhenMapped:
  3160.       return intern ("when-mapped");
  3161.  
  3162.     case NotUseful:
  3163.       return intern ("not-useful");
  3164.  
  3165.     default:
  3166.       error ("Strange value for BackingStore parameter of screen");
  3167.     }
  3168. }
  3169.  
  3170. DEFUN ("x-display-visual-class", Fx_display_visual_class,
  3171.   Sx_display_visual_class, 0, 1, 0,
  3172.   "Returns the visual class of the display `screen' is on.\n\
  3173. The value is one of the symbols `static-gray', `gray-scale',\n\
  3174. `static-color', `pseudo-color', `true-color', or `direct-color'.")
  3175.     (screen)
  3176.      Lisp_Object screen;
  3177. {
  3178.   check_x ();
  3179.  
  3180.   switch (screen_visual->class)
  3181.     {
  3182.     case StaticGray:  return (intern ("static-gray"));
  3183.     case GrayScale:   return (intern ("gray-scale"));
  3184.     case StaticColor: return (intern ("static-color"));
  3185.     case PseudoColor: return (intern ("pseudo-color"));
  3186.     case TrueColor:   return (intern ("true-color"));
  3187.     case DirectColor: return (intern ("direct-color"));
  3188.     default:
  3189.       error ("Display has an unknown visual class");
  3190.     }
  3191. }
  3192.  
  3193. DEFUN ("x-display-save-under", Fx_display_save_under,
  3194.   Sx_display_save_under, 0, 1, 0,
  3195.   "Returns t if the X screen FRAME is on supports the save-under feature.")
  3196.   (frame)
  3197.      Lisp_Object frame;
  3198. {
  3199.   check_x ();
  3200.  
  3201.   if (DoesSaveUnders (x_screen) == True)
  3202.     return Qt;
  3203.   else
  3204.     return Qnil;
  3205. }
  3206.  
  3207. x_pixel_width (f)
  3208.      register struct frame *f;
  3209. {
  3210.   return PIXEL_WIDTH (f);
  3211. }
  3212.  
  3213. x_pixel_height (f)
  3214.      register struct frame *f;
  3215. {
  3216.   return PIXEL_HEIGHT (f);
  3217. }
  3218.  
  3219. x_char_width (f)
  3220.      register struct frame *f;
  3221. {
  3222.   return FONT_WIDTH (f->display.x->font);
  3223. }
  3224.  
  3225. x_char_height (f)
  3226.      register struct frame *f;
  3227. {
  3228.   return f->display.x->line_height;
  3229. }
  3230.  
  3231. #if 0  /* These no longer seem like the right way to do things.  */
  3232.  
  3233. /* Draw a rectangle on the frame with left top corner including
  3234.    the character specified by LEFT_CHAR and TOP_CHAR.  The rectangle is
  3235.    CHARS by LINES wide and long and is the color of the cursor. */
  3236.  
  3237. void
  3238. x_rectangle (f, gc, left_char, top_char, chars, lines)
  3239.      register struct frame *f;
  3240.      GC gc;
  3241.      register int top_char, left_char, chars, lines;
  3242. {
  3243.   int width;
  3244.   int height;
  3245.   int left = (left_char * FONT_WIDTH (f->display.x->font)
  3246.             + f->display.x->internal_border_width);
  3247.   int top = (top_char * f->display.x->line_height
  3248.            + f->display.x->internal_border_width);
  3249.  
  3250.   if (chars < 0)
  3251.     width = FONT_WIDTH (f->display.x->font) / 2;
  3252.   else
  3253.     width = FONT_WIDTH (f->display.x->font) * chars;
  3254.   if (lines < 0)
  3255.     height = f->display.x->line_height / 2;
  3256.   else
  3257.     height = f->display.x->line_height * lines;
  3258.  
  3259.   XDrawRectangle (x_current_display, FRAME_X_WINDOW (f),
  3260.           gc, left, top, width, height);
  3261. }
  3262.  
  3263. DEFUN ("x-draw-rectangle", Fx_draw_rectangle, Sx_draw_rectangle, 5, 5, 0,
  3264.   "Draw a rectangle on FRAME between coordinates specified by\n\
  3265. numbers X0, Y0, X1, Y1 in the cursor pixel.")
  3266.   (frame, X0, Y0, X1, Y1)
  3267.      register Lisp_Object frame, X0, X1, Y0, Y1;
  3268. {
  3269.   register int x0, y0, x1, y1, top, left, n_chars, n_lines;
  3270.  
  3271.   CHECK_LIVE_FRAME (frame, 0);
  3272.   CHECK_NUMBER (X0, 0);
  3273.   CHECK_NUMBER (Y0, 1);
  3274.   CHECK_NUMBER (X1, 2);
  3275.   CHECK_NUMBER (Y1, 3);
  3276.  
  3277.   x0 = XINT (X0);
  3278.   x1 = XINT (X1);
  3279.   y0 = XINT (Y0);
  3280.   y1 = XINT (Y1);
  3281.  
  3282.   if (y1 > y0)
  3283.     {
  3284.       top = y0;
  3285.       n_lines = y1 - y0 + 1;
  3286.     }
  3287.   else
  3288.     {
  3289.       top = y1;
  3290.       n_lines = y0 - y1 + 1;
  3291.     }
  3292.  
  3293.   if (x1 > x0)
  3294.     {
  3295.       left = x0;
  3296.       n_chars = x1 - x0 + 1;
  3297.     }
  3298.   else
  3299.     {
  3300.       left = x1;
  3301.       n_chars = x0 - x1 + 1;
  3302.     }
  3303.  
  3304.   BLOCK_INPUT;
  3305.   x_rectangle (XFRAME (frame), XFRAME (frame)->display.x->cursor_gc,
  3306.            left, top, n_chars, n_lines);
  3307.   UNBLOCK_INPUT;
  3308.  
  3309.   return Qt;
  3310. }
  3311.  
  3312. DEFUN ("x-erase-rectangle", Fx_erase_rectangle, Sx_erase_rectangle, 5, 5, 0,
  3313.   "Draw a rectangle drawn on FRAME between coordinates\n\
  3314. X0, Y0, X1, Y1 in the regular background-pixel.")
  3315.   (frame, X0, Y0, X1, Y1)
  3316.   register Lisp_Object frame, X0, Y0, X1, Y1;
  3317. {
  3318.   register int x0, y0, x1, y1, top, left, n_chars, n_lines;
  3319.  
  3320.   CHECK_FRAME (frame, 0);
  3321.   CHECK_NUMBER (X0, 0);
  3322.   CHECK_NUMBER (Y0, 1);
  3323.   CHECK_NUMBER (X1, 2);
  3324.   CHECK_NUMBER (Y1, 3);
  3325.  
  3326.   x0 = XINT (X0);
  3327.   x1 = XINT (X1);
  3328.   y0 = XINT (Y0);
  3329.   y1 = XINT (Y1);
  3330.  
  3331.   if (y1 > y0)
  3332.     {
  3333.       top = y0;
  3334.       n_lines = y1 - y0 + 1;
  3335.     }
  3336.   else
  3337.     {
  3338.       top = y1;
  3339.       n_lines = y0 - y1 + 1;
  3340.     }
  3341.  
  3342.   if (x1 > x0)
  3343.     {
  3344.       left = x0;
  3345.       n_chars = x1 - x0 + 1;
  3346.     }
  3347.   else
  3348.     {
  3349.       left = x1;
  3350.       n_chars = x0 - x1 + 1;
  3351.     }
  3352.  
  3353.   BLOCK_INPUT;
  3354.   x_rectangle (XFRAME (frame), XFRAME (frame)->display.x->reverse_gc,
  3355.            left, top, n_chars, n_lines);
  3356.   UNBLOCK_INPUT;
  3357.  
  3358.   return Qt;
  3359. }
  3360.  
  3361. /* Draw lines around the text region beginning at the character position
  3362.    TOP_X, TOP_Y and ending at BOTTOM_X and BOTTOM_Y.  GC specifies the
  3363.    pixel and line characteristics. */
  3364.  
  3365. #define line_len(line) (FRAME_CURRENT_GLYPHS (f)->used[(line)])
  3366.  
  3367. static void
  3368. outline_region (f, gc, top_x, top_y, bottom_x, bottom_y)
  3369.      register struct frame *f;
  3370.      GC gc;
  3371.      int  top_x, top_y, bottom_x, bottom_y;
  3372. {
  3373.   register int ibw = f->display.x->internal_border_width;
  3374.   register int font_w = FONT_WIDTH (f->display.x->font);
  3375.   register int font_h = f->display.x->line_height;
  3376.   int y = top_y;
  3377.   int x = line_len (y);
  3378.   XPoint *pixel_points
  3379.     = (XPoint *) alloca (((bottom_y - top_y + 2) * 4) * sizeof (XPoint));
  3380.   register XPoint *this_point = pixel_points;
  3381.  
  3382.   /* Do the horizontal top line/lines */
  3383.   if (top_x == 0)
  3384.     {
  3385.       this_point->x = ibw;
  3386.       this_point->y = ibw + (font_h * top_y);
  3387.       this_point++;
  3388.       if (x == 0)
  3389.     this_point->x = ibw + (font_w / 2); /* Half-size for newline chars. */
  3390.       else
  3391.     this_point->x = ibw + (font_w * x);
  3392.       this_point->y = (this_point - 1)->y;
  3393.     }
  3394.   else
  3395.     {
  3396.       this_point->x = ibw;
  3397.       this_point->y = ibw + (font_h * (top_y + 1));
  3398.       this_point++;
  3399.       this_point->x = ibw + (font_w * top_x);
  3400.       this_point->y = (this_point - 1)->y;
  3401.       this_point++;
  3402.       this_point->x = (this_point - 1)->x;
  3403.       this_point->y = ibw + (font_h * top_y);
  3404.       this_point++;
  3405.       this_point->x = ibw + (font_w * x);
  3406.       this_point->y = (this_point - 1)->y;
  3407.     }
  3408.  
  3409.   /* Now do the right side. */
  3410.   while (y < bottom_y)
  3411.     {                /* Right vertical edge */
  3412.       this_point++;
  3413.       this_point->x = (this_point - 1)->x;
  3414.       this_point->y = ibw + (font_h * (y + 1));
  3415.       this_point++;
  3416.  
  3417.       y++;            /* Horizontal connection to next line */
  3418.       x = line_len (y);
  3419.       if (x == 0)
  3420.     this_point->x = ibw + (font_w / 2);
  3421.       else
  3422.     this_point->x = ibw + (font_w * x);
  3423.  
  3424.       this_point->y = (this_point - 1)->y;
  3425.     }
  3426.  
  3427.   /* Now do the bottom and connect to the top left point. */
  3428.   this_point->x = ibw + (font_w * (bottom_x + 1));
  3429.  
  3430.   this_point++;
  3431.   this_point->x = (this_point - 1)->x;
  3432.   this_point->y = ibw + (font_h * (bottom_y + 1));
  3433.   this_point++;
  3434.   this_point->x = ibw;
  3435.   this_point->y = (this_point - 1)->y;
  3436.   this_point++;
  3437.   this_point->x = pixel_points->x;
  3438.   this_point->y = pixel_points->y;
  3439.  
  3440.   XDrawLines (x_current_display, FRAME_X_WINDOW (f),
  3441.           gc, pixel_points,
  3442.           (this_point - pixel_points + 1), CoordModeOrigin);
  3443. }
  3444.  
  3445. DEFUN ("x-contour-region", Fx_contour_region, Sx_contour_region, 1, 1, 0,
  3446.   "Highlight the region between point and the character under the mouse\n\
  3447. selected frame.")
  3448.   (event)
  3449.      register Lisp_Object event;
  3450. {
  3451.   register int x0, y0, x1, y1;
  3452.   register struct frame *f = selected_frame;
  3453.   register int p1, p2;
  3454.  
  3455.   CHECK_CONS (event, 0);
  3456.  
  3457.   BLOCK_INPUT;
  3458.   x0 = XINT (Fcar (Fcar (event)));
  3459.   y0 = XINT (Fcar (Fcdr (Fcar (event))));
  3460.  
  3461.   /* If the mouse is past the end of the line, don't that area. */
  3462.   /* ReWrite this... */
  3463.  
  3464.   x1 = f->cursor_x;
  3465.   y1 = f->cursor_y;
  3466.  
  3467.   if (y1 > y0)            /* point below mouse */
  3468.     outline_region (f, f->display.x->cursor_gc,
  3469.             x0, y0, x1, y1);
  3470.   else if (y1 < y0)        /* point above mouse */
  3471.     outline_region (f, f->display.x->cursor_gc,
  3472.             x1, y1, x0, y0);
  3473.   else                /* same line: draw horizontal rectangle */
  3474.     {
  3475.       if (x1 > x0)
  3476.     x_rectangle (f, f->display.x->cursor_gc,
  3477.              x0, y0, (x1 - x0 + 1), 1);
  3478.       else if (x1 < x0)
  3479.       x_rectangle (f, f->display.x->cursor_gc,
  3480.                x1, y1, (x0 - x1 + 1), 1);
  3481.     }
  3482.  
  3483.   XFlush (x_current_display);
  3484.   UNBLOCK_INPUT;
  3485.  
  3486.   return Qnil;
  3487. }
  3488.  
  3489. DEFUN ("x-uncontour-region", Fx_uncontour_region, Sx_uncontour_region, 1, 1, 0,
  3490.   "Erase any highlighting of the region between point and the character\n\
  3491. at X, Y on the selected frame.")
  3492.   (event)
  3493.      register Lisp_Object event;
  3494. {
  3495.   register int x0, y0, x1, y1;
  3496.   register struct frame *f = selected_frame;
  3497.  
  3498.   BLOCK_INPUT;
  3499.   x0 = XINT (Fcar (Fcar (event)));
  3500.   y0 = XINT (Fcar (Fcdr (Fcar (event))));
  3501.   x1 = f->cursor_x;
  3502.   y1 = f->cursor_y;
  3503.  
  3504.   if (y1 > y0)            /* point below mouse */
  3505.     outline_region (f, f->display.x->reverse_gc,
  3506.               x0, y0, x1, y1);
  3507.   else if (y1 < y0)        /* point above mouse */
  3508.     outline_region (f, f->display.x->reverse_gc,
  3509.               x1, y1, x0, y0);
  3510.   else                /* same line: draw horizontal rectangle */
  3511.     {
  3512.       if (x1 > x0)
  3513.     x_rectangle (f, f->display.x->reverse_gc,
  3514.              x0, y0, (x1 - x0 + 1), 1);
  3515.       else if (x1 < x0)
  3516.     x_rectangle (f, f->display.x->reverse_gc,
  3517.              x1, y1, (x0 - x1 + 1), 1);
  3518.     }
  3519.   UNBLOCK_INPUT;
  3520.  
  3521.   return Qnil;
  3522. }
  3523.  
  3524. #if 0
  3525. int contour_begin_x, contour_begin_y;
  3526. int contour_end_x, contour_end_y;
  3527. int contour_npoints;
  3528.  
  3529. /* Clip the top part of the contour lines down (and including) line Y_POS.
  3530.    If X_POS is in the middle (rather than at the end) of the line, drop
  3531.    down a line at that character. */
  3532.  
  3533. static void
  3534. clip_contour_top (y_pos, x_pos)
  3535. {
  3536.   register XPoint *begin = contour_lines[y_pos].top_left;
  3537.   register XPoint *end;
  3538.   register int npoints;
  3539.   register struct display_line *line = selected_frame->phys_lines[y_pos + 1];
  3540.  
  3541.   if (x_pos >= line->len - 1)    /* Draw one, straight horizontal line. */
  3542.     {
  3543.       end = contour_lines[y_pos].top_right;
  3544.       npoints = (end - begin + 1);
  3545.       XDrawLines (x_current_display, contour_window,
  3546.           contour_erase_gc, begin_erase, npoints, CoordModeOrigin);
  3547.  
  3548.       bcopy (end, begin + 1, contour_last_point - end + 1);
  3549.       contour_last_point -= (npoints - 2);
  3550.       XDrawLines (x_current_display, contour_window,
  3551.           contour_erase_gc, begin, 2, CoordModeOrigin);
  3552.       XFlush (x_current_display);
  3553.  
  3554.       /* Now, update contour_lines structure. */
  3555.     }
  3556.                 /* ______.         */
  3557.   else                /*       |________*/
  3558.     {
  3559.       register XPoint *p = begin + 1;
  3560.       end = contour_lines[y_pos].bottom_right;
  3561.       npoints = (end - begin + 1);
  3562.       XDrawLines (x_current_display, contour_window,
  3563.           contour_erase_gc, begin_erase, npoints, CoordModeOrigin);
  3564.  
  3565.       p->y = begin->y;
  3566.       p->x = ibw + (font_w * (x_pos + 1));
  3567.       p++;
  3568.       p->y = begin->y + font_h;
  3569.       p->x = (p - 1)->x;
  3570.       bcopy (end, begin + 3, contour_last_point - end + 1);
  3571.       contour_last_point -= (npoints - 5);
  3572.       XDrawLines (x_current_display, contour_window,
  3573.           contour_erase_gc, begin, 4, CoordModeOrigin);
  3574.       XFlush (x_current_display);
  3575.  
  3576.       /* Now, update contour_lines structure. */
  3577.     }
  3578. }
  3579.  
  3580. /* Erase the top horizontal lines of the contour, and then extend
  3581.    the contour upwards. */
  3582.  
  3583. static void
  3584. extend_contour_top (line)
  3585. {
  3586. }
  3587.  
  3588. static void
  3589. clip_contour_bottom (x_pos, y_pos)
  3590.      int x_pos, y_pos;
  3591. {
  3592. }
  3593.  
  3594. static void
  3595. extend_contour_bottom (x_pos, y_pos)
  3596. {
  3597. }
  3598.  
  3599. DEFUN ("x-select-region", Fx_select_region, Sx_select_region, 1, 1, "e",
  3600.   "")
  3601.   (event)
  3602.      Lisp_Object event;
  3603. {
  3604.  register struct frame *f = selected_frame;
  3605.  register int point_x = f->cursor_x;
  3606.  register int point_y = f->cursor_y;
  3607.  register int mouse_below_point;
  3608.  register Lisp_Object obj;
  3609.  register int x_contour_x, x_contour_y;
  3610.  
  3611.  x_contour_x = x_mouse_x;
  3612.  x_contour_y = x_mouse_y;
  3613.  if (x_contour_y > point_y || (x_contour_y == point_y
  3614.                    && x_contour_x > point_x))
  3615.    {
  3616.      mouse_below_point = 1;
  3617.      outline_region (f, f->display.x->cursor_gc, point_x, point_y,
  3618.              x_contour_x, x_contour_y);
  3619.    }
  3620.  else
  3621.    {
  3622.      mouse_below_point = 0;
  3623.      outline_region (f, f->display.x->cursor_gc, x_contour_x, x_contour_y,
  3624.              point_x, point_y);
  3625.    }
  3626.  
  3627.  while (1)
  3628.    {
  3629.      obj = read_char (-1, 0, 0, Qnil, 0);
  3630.      if (XTYPE (obj) != Lisp_Cons)
  3631.        break;
  3632.  
  3633.      if (mouse_below_point)
  3634.        {
  3635.      if (x_mouse_y <= point_y)                /* Flipped. */
  3636.        {
  3637.          mouse_below_point = 0;
  3638.  
  3639.          outline_region (f, f->display.x->reverse_gc, point_x, point_y,
  3640.                  x_contour_x, x_contour_y);
  3641.          outline_region (f, f->display.x->cursor_gc, x_mouse_x, x_mouse_y,
  3642.                  point_x, point_y);
  3643.        }
  3644.      else if (x_mouse_y < x_contour_y)      /* Bottom clipped. */
  3645.        {
  3646.          clip_contour_bottom (x_mouse_y);
  3647.        }
  3648.      else if (x_mouse_y > x_contour_y)      /* Bottom extended. */
  3649.        {
  3650.          extend_bottom_contour (x_mouse_y);
  3651.        }
  3652.  
  3653.      x_contour_x = x_mouse_x;
  3654.      x_contour_y = x_mouse_y;
  3655.        }
  3656.      else  /* mouse above or same line as point */
  3657.        {
  3658.      if (x_mouse_y >= point_y)          /* Flipped. */
  3659.        {
  3660.          mouse_below_point = 1;
  3661.  
  3662.          outline_region (f, f->display.x->reverse_gc,
  3663.                  x_contour_x, x_contour_y, point_x, point_y);
  3664.          outline_region (f, f->display.x->cursor_gc, point_x, point_y,
  3665.                  x_mouse_x, x_mouse_y);
  3666.        }
  3667.      else if (x_mouse_y > x_contour_y)      /* Top clipped. */
  3668.        {
  3669.          clip_contour_top (x_mouse_y);
  3670.        }
  3671.      else if (x_mouse_y < x_contour_y)      /* Top extended. */
  3672.        {
  3673.          extend_contour_top (x_mouse_y);
  3674.        }
  3675.        }
  3676.    }
  3677.  
  3678.  unread_command_event = obj;
  3679.  if (mouse_below_point)
  3680.    {
  3681.      contour_begin_x = point_x;
  3682.      contour_begin_y = point_y;
  3683.      contour_end_x = x_contour_x;
  3684.      contour_end_y = x_contour_y;
  3685.    }
  3686.  else
  3687.    {
  3688.      contour_begin_x = x_contour_x;
  3689.      contour_begin_y = x_contour_y;
  3690.      contour_end_x = point_x;
  3691.      contour_end_y = point_y;
  3692.    }
  3693. }
  3694. #endif
  3695.  
  3696. DEFUN ("x-horizontal-line", Fx_horizontal_line, Sx_horizontal_line, 1, 1, "e",
  3697.   "")
  3698.   (event)
  3699.      Lisp_Object event;
  3700. {
  3701.   register Lisp_Object obj;
  3702.   struct frame *f = selected_frame;
  3703.   register struct window *w = XWINDOW (selected_window);
  3704.   register GC line_gc = f->display.x->cursor_gc;
  3705.   register GC erase_gc = f->display.x->reverse_gc;
  3706. #if 0
  3707.   char dash_list[] = {6, 4, 6, 4};
  3708.   int dashes = 4;
  3709.   XGCValues gc_values;
  3710. #endif
  3711.   register int previous_y;
  3712.   register int line = (x_mouse_y + 1) * f->display.x->line_height
  3713.     + f->display.x->internal_border_width;
  3714.   register int left = f->display.x->internal_border_width
  3715.     + (w->left
  3716.        * FONT_WIDTH (f->display.x->font));
  3717.   register int right = left + (w->width
  3718.                    * FONT_WIDTH (f->display.x->font))
  3719.     - f->display.x->internal_border_width;
  3720.  
  3721. #if 0
  3722.   BLOCK_INPUT;
  3723.   gc_values.foreground = f->display.x->cursor_pixel;
  3724.   gc_values.background = f->display.x->background_pixel;
  3725.   gc_values.line_width = 1;
  3726.   gc_values.line_style = LineOnOffDash;
  3727.   gc_values.cap_style = CapRound;
  3728.   gc_values.join_style = JoinRound;
  3729.  
  3730.   line_gc = XCreateGC (x_current_display, FRAME_X_WINDOW (f),
  3731.                GCLineStyle | GCJoinStyle | GCCapStyle
  3732.                | GCLineWidth | GCForeground | GCBackground,
  3733.                &gc_values);
  3734.   XSetDashes (x_current_display, line_gc, 0, dash_list, dashes);
  3735.   gc_values.foreground = f->display.x->background_pixel;
  3736.   gc_values.background = f->display.x->foreground_pixel;
  3737.   erase_gc = XCreateGC (x_current_display, FRAME_X_WINDOW (f),
  3738.                GCLineStyle | GCJoinStyle | GCCapStyle
  3739.                | GCLineWidth | GCForeground | GCBackground,
  3740.                &gc_values);
  3741.   XSetDashes (x_current_display, erase_gc, 0, dash_list, dashes);
  3742. #endif
  3743.  
  3744.   while (1)
  3745.     {
  3746.       BLOCK_INPUT;
  3747.       if (x_mouse_y >= XINT (w->top)
  3748.       && x_mouse_y < XINT (w->top) + XINT (w->height) - 1)
  3749.     {
  3750.       previous_y = x_mouse_y;
  3751.       line = (x_mouse_y + 1) * f->display.x->line_height
  3752.         + f->display.x->internal_border_width;
  3753.       XDrawLine (x_current_display, FRAME_X_WINDOW (f),
  3754.              line_gc, left, line, right, line);
  3755.     }
  3756.       XFlushQueue ();
  3757.       UNBLOCK_INPUT;
  3758.  
  3759.       do
  3760.     {
  3761.       obj = read_char (-1, 0, 0, Qnil, 0);
  3762.       if ((XTYPE (obj) != Lisp_Cons)
  3763.           || (! EQ (Fcar (Fcdr (Fcdr (obj))),
  3764.                Qvertical_scroll_bar))
  3765.           || x_mouse_grabbed)
  3766.         {
  3767.           BLOCK_INPUT;
  3768.           XDrawLine (x_current_display, FRAME_X_WINDOW (f),
  3769.              erase_gc, left, line, right, line);
  3770.           UNBLOCK_INPUT;
  3771.           unread_command_event = obj;
  3772. #if 0
  3773.           XFreeGC (x_current_display, line_gc);
  3774.           XFreeGC (x_current_display, erase_gc);
  3775. #endif 
  3776.           return Qnil;
  3777.         }
  3778.     }
  3779.       while (x_mouse_y == previous_y);
  3780.  
  3781.       BLOCK_INPUT;
  3782.       XDrawLine (x_current_display, FRAME_X_WINDOW (f),
  3783.          erase_gc, left, line, right, line);
  3784.       UNBLOCK_INPUT;
  3785.     }
  3786. }
  3787. #endif
  3788.  
  3789. /* Offset in buffer of character under the pointer, or 0. */
  3790. int mouse_buffer_offset;
  3791.  
  3792. #if 0
  3793. /* These keep track of the rectangle following the pointer. */
  3794. int mouse_track_top, mouse_track_left, mouse_track_width;
  3795.  
  3796. DEFUN ("x-track-pointer", Fx_track_pointer, Sx_track_pointer, 0, 0, 0,
  3797.   "Track the pointer.")
  3798.   ()
  3799. {
  3800.   static Cursor current_pointer_shape;
  3801.   FRAME_PTR f = x_mouse_frame;
  3802.  
  3803.   BLOCK_INPUT;
  3804.   if (EQ (Vmouse_frame_part, Qtext_part)
  3805.       && (current_pointer_shape != f->display.x->nontext_cursor))
  3806.     {
  3807.       unsigned char c;
  3808.       struct buffer *buf;
  3809.  
  3810.       current_pointer_shape = f->display.x->nontext_cursor;
  3811.       XDefineCursor (x_current_display,
  3812.              FRAME_X_WINDOW (f),
  3813.              current_pointer_shape);
  3814.  
  3815.       buf = XBUFFER (XWINDOW (Vmouse_window)->buffer);
  3816.       c = *(BUF_CHAR_ADDRESS (buf, mouse_buffer_offset));
  3817.     }
  3818.   else if (EQ (Vmouse_frame_part, Qmodeline_part)
  3819.        && (current_pointer_shape != f->display.x->modeline_cursor))
  3820.     {
  3821.       current_pointer_shape = f->display.x->modeline_cursor;
  3822.       XDefineCursor (x_current_display,
  3823.              FRAME_X_WINDOW (f),
  3824.              current_pointer_shape);
  3825.     }
  3826.  
  3827.   XFlushQueue ();
  3828.   UNBLOCK_INPUT;
  3829. }
  3830. #endif
  3831.  
  3832. #if 0
  3833. DEFUN ("x-track-pointer", Fx_track_pointer, Sx_track_pointer, 1, 1, "e",
  3834.   "Draw rectangle around character under mouse pointer, if there is one.")
  3835.   (event)
  3836.      Lisp_Object event;
  3837. {
  3838.   struct window *w = XWINDOW (Vmouse_window);
  3839.   struct frame *f = XFRAME (WINDOW_FRAME (w));
  3840.   struct buffer *b = XBUFFER (w->buffer);
  3841.   Lisp_Object obj;
  3842.  
  3843.   if (! EQ (Vmouse_window, selected_window))
  3844.       return Qnil;
  3845.  
  3846.   if (EQ (event, Qnil))
  3847.     {
  3848.       int x, y;
  3849.  
  3850.       x_read_mouse_position (selected_frame, &x, &y);
  3851.     }
  3852.  
  3853.   BLOCK_INPUT;
  3854.   mouse_track_width = 0;
  3855.   mouse_track_left = mouse_track_top = -1;
  3856.  
  3857.   do
  3858.     {
  3859.       if ((x_mouse_x != mouse_track_left
  3860.        && (x_mouse_x < mouse_track_left
  3861.            || x_mouse_x > (mouse_track_left + mouse_track_width)))
  3862.       || x_mouse_y != mouse_track_top)
  3863.     {
  3864.       int hp = 0;        /* Horizontal position */
  3865.       int len = FRAME_CURRENT_GLYPHS (f)->used[x_mouse_y];
  3866.       int p = FRAME_CURRENT_GLYPHS (f)->bufp[x_mouse_y];
  3867.       int tab_width = XINT (b->tab_width);
  3868.       int ctl_arrow_p = !NILP (b->ctl_arrow);
  3869.       unsigned char c;
  3870.       int mode_line_vpos = XFASTINT (w->height) + XFASTINT (w->top) - 1;
  3871.       int in_mode_line = 0;
  3872.  
  3873.       if (! FRAME_CURRENT_GLYPHS (f)->enable[x_mouse_y])
  3874.         break;
  3875.  
  3876.       /* Erase previous rectangle. */
  3877.       if (mouse_track_width)
  3878.         {
  3879.           x_rectangle (f, f->display.x->reverse_gc,
  3880.                mouse_track_left, mouse_track_top,
  3881.                mouse_track_width, 1);
  3882.  
  3883.           if ((mouse_track_left == f->phys_cursor_x
  3884.            || mouse_track_left == f->phys_cursor_x - 1)
  3885.           && mouse_track_top == f->phys_cursor_y)
  3886.         {
  3887.           x_display_cursor (f, 1);
  3888.         }
  3889.         }
  3890.  
  3891.       mouse_track_left = x_mouse_x;
  3892.       mouse_track_top = x_mouse_y;
  3893.       mouse_track_width = 0;
  3894.  
  3895.       if (mouse_track_left > len) /* Past the end of line. */
  3896.         goto draw_or_not;
  3897.  
  3898.       if (mouse_track_top == mode_line_vpos)
  3899.         {
  3900.           in_mode_line = 1;
  3901.           goto draw_or_not;
  3902.         }
  3903.  
  3904.       if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  3905.       do
  3906.         {
  3907.           c = FETCH_CHAR (p);
  3908.           if (len == f->width && hp == len - 1 && c != '\n')
  3909.         goto draw_or_not;
  3910.  
  3911.           switch (c)
  3912.         {
  3913.         case '\t':
  3914.           mouse_track_width = tab_width - (hp % tab_width);
  3915.           p++;
  3916.           hp += mouse_track_width;
  3917.           if (hp > x_mouse_x)
  3918.             {
  3919.               mouse_track_left = hp - mouse_track_width;
  3920.               goto draw_or_not;
  3921.             }
  3922.           continue;
  3923.  
  3924.         case '\n':
  3925.           mouse_track_width = -1;
  3926.           goto draw_or_not;
  3927.  
  3928.         default:
  3929.           if (ctl_arrow_p && (c < 040 || c == 0177))
  3930.             {
  3931.               if (p > ZV)
  3932.             goto draw_or_not;
  3933.  
  3934.               mouse_track_width = 2;
  3935.               p++;
  3936.               hp +=2;
  3937.               if (hp > x_mouse_x)
  3938.             {
  3939.               mouse_track_left = hp - mouse_track_width;
  3940.               goto draw_or_not;
  3941.             }
  3942.             }
  3943.           else
  3944.             {
  3945.               mouse_track_width = 1;
  3946.               p++;
  3947.               hp++;
  3948.             }
  3949.           continue;
  3950.         }
  3951.         }
  3952.       while (hp <= x_mouse_x);
  3953.  
  3954.     draw_or_not:
  3955.       if (mouse_track_width) /* Over text; use text pointer shape. */
  3956.         {
  3957.           XDefineCursor (x_current_display,
  3958.                  FRAME_X_WINDOW (f),
  3959.                  f->display.x->text_cursor);
  3960.           x_rectangle (f, f->display.x->cursor_gc,
  3961.                mouse_track_left, mouse_track_top,
  3962.                mouse_track_width, 1);
  3963.         }
  3964.       else if (in_mode_line)
  3965.         XDefineCursor (x_current_display,
  3966.                FRAME_X_WINDOW (f),
  3967.                f->display.x->modeline_cursor);
  3968.       else
  3969.         XDefineCursor (x_current_display,
  3970.                FRAME_X_WINDOW (f),
  3971.                f->display.x->nontext_cursor);
  3972.     }
  3973.  
  3974.       XFlush (x_current_display);
  3975.       UNBLOCK_INPUT;
  3976.  
  3977.       obj = read_char (-1, 0, 0, Qnil, 0);
  3978.       BLOCK_INPUT;
  3979.     }
  3980.   while (XTYPE (obj) == Lisp_Cons           /* Mouse event */
  3981.      && EQ (Fcar (Fcdr (Fcdr (obj))), Qnil)       /* Not scroll bar */
  3982.      && EQ (Vmouse_depressed, Qnil)              /* Only motion events */
  3983.      && EQ (Vmouse_window, selected_window)       /* In this window */
  3984.      && x_mouse_frame);
  3985.  
  3986.   unread_command_event = obj;
  3987.  
  3988.   if (mouse_track_width)
  3989.     {
  3990.       x_rectangle (f, f->display.x->reverse_gc,
  3991.            mouse_track_left, mouse_track_top,
  3992.            mouse_track_width, 1);
  3993.       mouse_track_width = 0;
  3994.       if ((mouse_track_left == f->phys_cursor_x
  3995.        || mouse_track_left - 1 == f->phys_cursor_x)
  3996.       && mouse_track_top == f->phys_cursor_y)
  3997.     {
  3998.       x_display_cursor (f, 1);
  3999.     }
  4000.     }
  4001.   XDefineCursor (x_current_display,
  4002.          FRAME_X_WINDOW (f),
  4003.          f->display.x->nontext_cursor);
  4004.   XFlush (x_current_display);
  4005.   UNBLOCK_INPUT;
  4006.  
  4007.   return Qnil;
  4008. }
  4009. #endif
  4010.  
  4011. #if 0
  4012. #include "glyphs.h"
  4013.  
  4014. /* Draw a pixmap specified by IMAGE_DATA of dimensions WIDTH and HEIGHT
  4015.    on the frame F at position X, Y. */
  4016.  
  4017. x_draw_pixmap (f, x, y, image_data, width, height)
  4018.      struct frame *f;
  4019.      int x, y, width, height;
  4020.      char *image_data;
  4021. {
  4022.   Pixmap image;
  4023.  
  4024.   image = XCreateBitmapFromData (x_current_display,
  4025.                  FRAME_X_WINDOW (f), image_data,
  4026.                  width, height);
  4027.   XCopyPlane (x_current_display, image, FRAME_X_WINDOW (f),
  4028.           f->display.x->normal_gc, 0, 0, width, height, x, y);
  4029. }
  4030. #endif
  4031.  
  4032. #ifndef HAVE_X11
  4033. DEFUN ("x-store-cut-buffer", Fx_store_cut_buffer, Sx_store_cut_buffer,
  4034.   1, 1, "sStore text in cut buffer: ",
  4035.   "Store contents of STRING into the cut buffer of the X window system.")
  4036.   (string)
  4037.      register Lisp_Object string;
  4038. {
  4039.   int mask;
  4040.  
  4041.   CHECK_STRING (string, 1);
  4042.   if (! FRAME_X_P (selected_frame))
  4043.     error ("Selected frame does not understand X protocol.");
  4044.  
  4045.   BLOCK_INPUT;
  4046.   XStoreBytes ((char *) XSTRING (string)->data, XSTRING (string)->size);
  4047.   UNBLOCK_INPUT;
  4048.  
  4049.   return Qnil;
  4050. }
  4051.  
  4052. DEFUN ("x-get-cut-buffer", Fx_get_cut_buffer, Sx_get_cut_buffer, 0, 0, 0,
  4053.   "Return contents of cut buffer of the X window system, as a string.")
  4054.   ()
  4055. {
  4056.   int len;
  4057.   register Lisp_Object string;
  4058.   int mask;
  4059.   register char *d;
  4060.  
  4061.   BLOCK_INPUT;
  4062.   d = XFetchBytes (&len);
  4063.   string = make_string (d, len);
  4064.   XFree (d);
  4065.   UNBLOCK_INPUT;
  4066.   return string;
  4067. }
  4068. #endif    /* X10 */
  4069.  
  4070. #if 0 /* I'm told these functions are superfluous
  4071.      given the ability to bind function keys.  */
  4072.  
  4073. #ifdef HAVE_X11
  4074. DEFUN ("x-rebind-key", Fx_rebind_key, Sx_rebind_key, 3, 3, 0,
  4075. "Rebind X keysym KEYSYM, with MODIFIERS, to generate NEWSTRING.\n\
  4076. KEYSYM is a string which conforms to the X keysym definitions found\n\
  4077. in X11/keysymdef.h, sans the initial XK_. MODIFIERS is nil or a\n\
  4078. list of strings specifying modifier keys such as Control_L, which must\n\
  4079. also be depressed for NEWSTRING to appear.")
  4080.   (x_keysym, modifiers, newstring)
  4081.      register Lisp_Object x_keysym;
  4082.      register Lisp_Object modifiers;
  4083.      register Lisp_Object newstring;
  4084. {
  4085.   char *rawstring;
  4086.   register KeySym keysym;
  4087.   KeySym modifier_list[16];
  4088.  
  4089.   check_x ();
  4090.   CHECK_STRING (x_keysym, 1);
  4091.   CHECK_STRING (newstring, 3);
  4092.  
  4093.   keysym = XStringToKeysym ((char *) XSTRING (x_keysym)->data);
  4094.   if (keysym == NoSymbol)
  4095.     error ("Keysym does not exist");
  4096.  
  4097.   if (NILP (modifiers))
  4098.     XRebindKeysym (x_current_display, keysym, modifier_list, 0,
  4099.            XSTRING (newstring)->data, XSTRING (newstring)->size);
  4100.   else
  4101.     {
  4102.       register Lisp_Object rest, mod;
  4103.       register int i = 0;
  4104.  
  4105.       for (rest = modifiers; !NILP (rest); rest = Fcdr (rest))
  4106.     {
  4107.       if (i == 16)
  4108.         error ("Can't have more than 16 modifiers");
  4109.  
  4110.       mod = Fcar (rest);
  4111.       CHECK_STRING (mod, 3);
  4112.       modifier_list[i] = XStringToKeysym ((char *) XSTRING (mod)->data);
  4113. #ifndef HAVE_X11R5
  4114.       if (modifier_list[i] == NoSymbol
  4115.           || !(IsModifierKey (modifier_list[i]) 
  4116.                    || ((unsigned)(modifier_list[i]) == XK_Mode_switch)
  4117.                    || ((unsigned)(modifier_list[i]) == XK_Num_Lock)))
  4118. #else
  4119.       if (modifier_list[i] == NoSymbol
  4120.           || !IsModifierKey (modifier_list[i]))
  4121. #endif
  4122.         error ("Element is not a modifier keysym");
  4123.       i++;
  4124.     }
  4125.  
  4126.       XRebindKeysym (x_current_display, keysym, modifier_list, i,
  4127.              XSTRING (newstring)->data, XSTRING (newstring)->size);
  4128.     }
  4129.  
  4130.   return Qnil;
  4131. }
  4132.   
  4133. DEFUN ("x-rebind-keys", Fx_rebind_keys, Sx_rebind_keys, 2, 2, 0,
  4134.   "Rebind KEYCODE to list of strings STRINGS.\n\
  4135. STRINGS should be a list of 16 elements, one for each shift combination.\n\
  4136. nil as element means don't change.\n\
  4137. See the documentation of `x-rebind-key' for more information.")
  4138.   (keycode, strings)
  4139.      register Lisp_Object keycode;
  4140.      register Lisp_Object strings;
  4141. {
  4142.   register Lisp_Object item;
  4143.   register unsigned char *rawstring;
  4144.   KeySym rawkey, modifier[1];
  4145.   int strsize;
  4146.   register unsigned i;
  4147.  
  4148.   check_x ();
  4149.   CHECK_NUMBER (keycode, 1);
  4150.   CHECK_CONS (strings, 2);
  4151.   rawkey = (KeySym) ((unsigned) (XINT (keycode))) & 255;
  4152.   for (i = 0; i <= 15; strings = Fcdr (strings), i++)
  4153.     {
  4154.       item = Fcar (strings);
  4155.       if (!NILP (item))
  4156.     {
  4157.       CHECK_STRING (item, 2);
  4158.       strsize = XSTRING (item)->size;
  4159.       rawstring = (unsigned char *) xmalloc (strsize);
  4160.       bcopy (XSTRING (item)->data, rawstring, strsize);
  4161.       modifier[1] = 1 << i;
  4162.       XRebindKeysym (x_current_display, rawkey, modifier, 1,
  4163.              rawstring, strsize);
  4164.     }
  4165.     }
  4166.   return Qnil;
  4167. }
  4168. #endif /* HAVE_X11 */
  4169. #endif /* 0 */
  4170.  
  4171. #ifdef HAVE_X11
  4172.  
  4173. #ifndef HAVE_XSCREENNUMBEROFSCREEN
  4174. int
  4175. XScreenNumberOfScreen (scr)
  4176.     register Screen *scr;
  4177. {
  4178.   register Display *dpy;
  4179.   register Screen *dpyscr;
  4180.   register int i;
  4181.  
  4182.   dpy = scr->display;
  4183.   dpyscr = dpy->screens;
  4184.  
  4185.   for (i = 0; i < dpy->nscreens; i++, dpyscr++)
  4186.     if (scr == dpyscr)
  4187.       return i;
  4188.  
  4189.   return -1;
  4190. }
  4191. #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
  4192.  
  4193. Visual *
  4194. select_visual (screen, depth)
  4195.      Screen *screen;
  4196.      unsigned int *depth;
  4197. {
  4198.   Visual *v;
  4199.   XVisualInfo *vinfo, vinfo_template;
  4200.   int n_visuals;
  4201.  
  4202.   v = DefaultVisualOfScreen (screen);
  4203.  
  4204. #ifdef HAVE_X11R4
  4205.   vinfo_template.visualid = XVisualIDFromVisual (v);
  4206. #else
  4207.   vinfo_template.visualid = v->visualid;
  4208. #endif
  4209.  
  4210.   vinfo_template.screen = XScreenNumberOfScreen (screen);
  4211.  
  4212.   vinfo = XGetVisualInfo (x_current_display,
  4213.               VisualIDMask | VisualScreenMask, &vinfo_template,
  4214.               &n_visuals);
  4215.   if (n_visuals != 1)
  4216.     fatal ("Can't get proper X visual info");
  4217.  
  4218.   if ((1 << vinfo->depth) == vinfo->colormap_size)
  4219.     *depth = vinfo->depth;
  4220.   else
  4221.     {
  4222.       int i = 0;
  4223.       int n = vinfo->colormap_size - 1;
  4224.       while (n)
  4225.     {
  4226.       n = n >> 1;
  4227.       i++;
  4228.     }
  4229.       *depth = i;
  4230.     }
  4231.  
  4232.   XFree ((char *) vinfo);
  4233.   return v;
  4234. }
  4235. #endif /* HAVE_X11 */
  4236.  
  4237. DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
  4238.        1, 2, 0, "Open a connection to an X server.\n\
  4239. DISPLAY is the name of the display to connect to.\n\
  4240. Optional second arg XRM_STRING is a string of resources in xrdb format.")
  4241.   (display, xrm_string)
  4242.      Lisp_Object display, xrm_string;
  4243. {
  4244.   unsigned int n_planes;
  4245.   unsigned char *xrm_option;
  4246.  
  4247.   CHECK_STRING (display, 0);
  4248.   if (x_current_display != 0)
  4249.     error ("X server connection is already initialized");
  4250.   if (! NILP (xrm_string))
  4251.     CHECK_STRING (xrm_string, 1);
  4252.  
  4253.   if (! NILP (xrm_string))
  4254.     xrm_option = (unsigned char *) XSTRING (xrm_string)->data;
  4255.   else
  4256.     xrm_option = (unsigned char *) 0;
  4257.  
  4258.   validate_x_resource_name ();
  4259.  
  4260.   /* This is what opens the connection and sets x_current_display.
  4261.      This also initializes many symbols, such as those used for input. */
  4262.   x_term_init (XSTRING (display)->data, xrm_option,
  4263.            XSTRING (Vx_resource_name)->data);
  4264.  
  4265. #ifdef HAVE_X11
  4266.   XFASTINT (Vwindow_system_version) = 11;
  4267.  
  4268.   BLOCK_INPUT;
  4269.   xrdb = x_load_resources (x_current_display, xrm_option,
  4270.                (char *) XSTRING (Vx_resource_name)->data,
  4271.                EMACS_CLASS);
  4272.   UNBLOCK_INPUT;
  4273. #ifdef HAVE_XRMSETDATABASE
  4274.   XrmSetDatabase (x_current_display, xrdb);
  4275. #else
  4276.   x_current_display->db = xrdb;
  4277. #endif
  4278.  
  4279.   x_screen = DefaultScreenOfDisplay (x_current_display);
  4280.  
  4281.   screen_visual = select_visual (x_screen, &n_planes);
  4282.   x_screen_planes = n_planes;
  4283.   x_screen_height = HeightOfScreen (x_screen);
  4284.   x_screen_width = WidthOfScreen (x_screen);
  4285.  
  4286.   /* X Atoms used by emacs. */
  4287.   Xatoms_of_xselect ();
  4288.   BLOCK_INPUT;
  4289.   Xatom_wm_protocols =     XInternAtom (x_current_display, "WM_PROTOCOLS",
  4290.                     False);
  4291.   Xatom_wm_take_focus =    XInternAtom (x_current_display, "WM_TAKE_FOCUS",
  4292.                     False);
  4293.   Xatom_wm_save_yourself = XInternAtom (x_current_display, "WM_SAVE_YOURSELF",
  4294.                     False);
  4295.   Xatom_wm_delete_window = XInternAtom (x_current_display, "WM_DELETE_WINDOW",
  4296.                     False);
  4297.   Xatom_wm_change_state =  XInternAtom (x_current_display, "WM_CHANGE_STATE",
  4298.                     False);
  4299.   Xatom_wm_configure_denied =  XInternAtom (x_current_display,
  4300.                         "WM_CONFIGURE_DENIED", False);
  4301.   Xatom_wm_window_moved =  XInternAtom (x_current_display, "WM_MOVED",
  4302.                     False);
  4303.   Xatom_editres_name =  XInternAtom (x_current_display, "Editres", False);
  4304.   UNBLOCK_INPUT;
  4305. #else /* not HAVE_X11 */
  4306.   XFASTINT (Vwindow_system_version) = 10;
  4307. #endif /* not HAVE_X11 */
  4308.   return Qnil;
  4309. }
  4310.  
  4311. DEFUN ("x-close-current-connection", Fx_close_current_connection,
  4312.        Sx_close_current_connection,
  4313.        0, 0, 0, "Close the connection to the current X server.")
  4314.   ()
  4315. {
  4316.   /* Note: If we're going to call check_x here, then the fatal error
  4317.      can't happen.  For the moment, this check is just for safety,
  4318.      so a user won't try out the function and get a crash.  If it's
  4319.      really intended only to be called when killing emacs, then there's
  4320.      no reason for it to have a lisp interface at all.  */
  4321.   check_x();
  4322. #ifdef HAVE_X11
  4323.   /* This is ONLY used when killing emacs;  For switching displays
  4324.      we'll have to take care of setting CloseDownMode elsewhere. */
  4325.  
  4326.   if (x_current_display)
  4327.     {
  4328.       BLOCK_INPUT;
  4329.       XSetCloseDownMode (x_current_display, DestroyAll);
  4330.       XCloseDisplay (x_current_display);
  4331.       x_current_display = 0;
  4332.     }
  4333.   else
  4334.     fatal ("No current X display connection to close\n");
  4335. #endif
  4336.   return Qnil;
  4337. }
  4338.  
  4339. DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize,
  4340.        1, 1, 0, "If ON is non-nil, report X errors as soon as the erring request is made.\n\
  4341. If ON is nil, allow buffering of requests.\n\
  4342. Turning on synchronization prohibits the Xlib routines from buffering\n\
  4343. requests and seriously degrades performance, but makes debugging much\n\
  4344. easier.")
  4345.   (on)
  4346.     Lisp_Object on;
  4347. {
  4348.   check_x ();
  4349.  
  4350.   XSynchronize (x_current_display, !EQ (on, Qnil));
  4351.  
  4352.   return Qnil;
  4353. }
  4354.  
  4355. /* Wait for responses to all X commands issued so far for FRAME.  */
  4356.  
  4357. void
  4358. x_sync (frame)
  4359.      Lisp_Object frame;
  4360. {
  4361.   BLOCK_INPUT;
  4362.   XSync (x_current_display, False);
  4363.   UNBLOCK_INPUT;
  4364. }
  4365.  
  4366. syms_of_xfns ()
  4367. {
  4368.   /* This is zero if not using X windows.  */
  4369.   x_current_display = 0;
  4370.  
  4371.   /* The section below is built by the lisp expression at the top of the file,
  4372.      just above where these variables are declared.  */
  4373.   /*&&& init symbols here &&&*/
  4374.   Qauto_raise = intern ("auto-raise");
  4375.   staticpro (&Qauto_raise);
  4376.   Qauto_lower = intern ("auto-lower");
  4377.   staticpro (&Qauto_lower);
  4378.   Qbackground_color = intern ("background-color");
  4379.   staticpro (&Qbackground_color);
  4380.   Qbar = intern ("bar");
  4381.   staticpro (&Qbar);
  4382.   Qborder_color = intern ("border-color");
  4383.   staticpro (&Qborder_color);
  4384.   Qborder_width = intern ("border-width");
  4385.   staticpro (&Qborder_width);
  4386.   Qbox = intern ("box");
  4387.   staticpro (&Qbox);
  4388.   Qcursor_color = intern ("cursor-color");
  4389.   staticpro (&Qcursor_color);
  4390.   Qcursor_type = intern ("cursor-type");
  4391.   staticpro (&Qcursor_type);
  4392.   Qfont = intern ("font");
  4393.   staticpro (&Qfont);
  4394.   Qforeground_color = intern ("foreground-color");
  4395.   staticpro (&Qforeground_color);
  4396.   Qgeometry = intern ("geometry");
  4397.   staticpro (&Qgeometry);
  4398.   Qicon_left = intern ("icon-left");
  4399.   staticpro (&Qicon_left);
  4400.   Qicon_top = intern ("icon-top");
  4401.   staticpro (&Qicon_top);
  4402.   Qicon_type = intern ("icon-type");
  4403.   staticpro (&Qicon_type);
  4404.   Qinternal_border_width = intern ("internal-border-width");
  4405.   staticpro (&Qinternal_border_width);
  4406.   Qleft = intern ("left");
  4407.   staticpro (&Qleft);
  4408.   Qmouse_color = intern ("mouse-color");
  4409.   staticpro (&Qmouse_color);
  4410.   Qnone = intern ("none");
  4411.   staticpro (&Qnone);
  4412.   Qparent_id = intern ("parent-id");
  4413.   staticpro (&Qparent_id);
  4414.   Qsuppress_icon = intern ("suppress-icon");
  4415.   staticpro (&Qsuppress_icon);
  4416.   Qtop = intern ("top");
  4417.   staticpro (&Qtop);
  4418.   Qundefined_color = intern ("undefined-color");
  4419.   staticpro (&Qundefined_color);
  4420.   Qvertical_scroll_bars = intern ("vertical-scroll-bars");
  4421.   staticpro (&Qvertical_scroll_bars);
  4422.   Qvisibility = intern ("visibility");
  4423.   staticpro (&Qvisibility);
  4424.   Qwindow_id = intern ("window-id");
  4425.   staticpro (&Qwindow_id);
  4426.   Qx_frame_parameter = intern ("x-frame-parameter");
  4427.   staticpro (&Qx_frame_parameter);
  4428.   Qx_resource_name = intern ("x-resource-name");
  4429.   staticpro (&Qx_resource_name);
  4430.   Quser_position = intern ("user-position");
  4431.   staticpro (&Quser_position);
  4432.   Quser_size = intern ("user-size");
  4433.   staticpro (&Quser_size);
  4434.   /* This is the end of symbol initialization.  */
  4435.  
  4436.   Fput (Qundefined_color, Qerror_conditions,
  4437.     Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
  4438.   Fput (Qundefined_color, Qerror_message,
  4439.     build_string ("Undefined color"));
  4440.  
  4441.   init_x_parm_symbols ();
  4442.  
  4443.   DEFVAR_INT ("mouse-buffer-offset", &mouse_buffer_offset,
  4444.     "The buffer offset of the character under the pointer.");
  4445.   mouse_buffer_offset = 0;
  4446.  
  4447.   DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
  4448.     "The shape of the pointer when over text.\n\
  4449. Changing the value does not affect existing frames\n\
  4450. unless you set the mouse color.");
  4451.   Vx_pointer_shape = Qnil;
  4452.  
  4453.   DEFVAR_LISP ("x-resource-name", &Vx_resource_name,
  4454.     "The name Emacs uses to look up X resources; for internal use only.\n\
  4455. `x-get-resource' uses this as the first component of the instance name\n\
  4456. when requesting resource values.\n\
  4457. Emacs initially sets `x-resource-name' to the name under which Emacs\n\
  4458. was invoked, or to the value specified with the `-name' or `-rn'\n\
  4459. switches, if present.");
  4460.   Vx_resource_name = Qnil;
  4461.  
  4462. #if 0 /* This doesn't really do anything.  */
  4463.   DEFVAR_INT ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape,
  4464.           "The shape of the pointer when not over text.\n\
  4465. This variable takes effect when you create a new frame\n\
  4466. or when you set the mouse color.");
  4467. #endif
  4468.   Vx_nontext_pointer_shape = Qnil;
  4469.  
  4470. #if 0 /* This doesn't really do anything.  */
  4471.   DEFVAR_INT ("x-mode-pointer-shape", &Vx_mode_pointer_shape,
  4472.           "The shape of the pointer when over the mode line.\n\
  4473. This variable takes effect when you create a new frame\n\
  4474. or when you set the mouse color.");
  4475. #endif
  4476.   Vx_mode_pointer_shape = Qnil;
  4477.  
  4478.   DEFVAR_INT ("x-sensitive-text-pointer-shape",
  4479.           &Vx_sensitive_text_pointer_shape,
  4480.           "The shape of the pointer when over mouse-sensitive text.\n\
  4481. This variable takes effect when you create a new frame\n\
  4482. or when you set the mouse color.");
  4483.   Vx_sensitive_text_pointer_shape = Qnil;
  4484.  
  4485.   DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
  4486.            "A string indicating the foreground color of the cursor box.");
  4487.   Vx_cursor_fore_pixel = Qnil;
  4488.  
  4489.   DEFVAR_LISP ("mouse-grabbed", &Vmouse_depressed,
  4490.            "Non-nil if a mouse button is currently depressed.");
  4491.   Vmouse_depressed = Qnil;
  4492.  
  4493.   DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
  4494.            "Non-nil if no X window manager is in use.");
  4495.  
  4496. #ifdef USE_X_TOOLKIT
  4497.   Fprovide (intern ("x-toolkit"));
  4498. #endif
  4499.  
  4500. #ifdef HAVE_X11
  4501.   defsubr (&Sx_get_resource);
  4502. #if 0
  4503.   defsubr (&Sx_draw_rectangle);
  4504.   defsubr (&Sx_erase_rectangle);
  4505.   defsubr (&Sx_contour_region);
  4506.   defsubr (&Sx_uncontour_region);
  4507. #endif
  4508.   defsubr (&Sx_display_color_p);
  4509.   defsubr (&Sx_list_fonts);
  4510.   defsubr (&Sx_color_defined_p);
  4511.   defsubr (&Sx_color_values);
  4512.   defsubr (&Sx_server_max_request_size);
  4513.   defsubr (&Sx_server_vendor);
  4514.   defsubr (&Sx_server_version);
  4515.   defsubr (&Sx_display_pixel_width);
  4516.   defsubr (&Sx_display_pixel_height);
  4517.   defsubr (&Sx_display_mm_width);
  4518.   defsubr (&Sx_display_mm_height);
  4519.   defsubr (&Sx_display_screens);
  4520.   defsubr (&Sx_display_planes);
  4521.   defsubr (&Sx_display_color_cells);
  4522.   defsubr (&Sx_display_visual_class);
  4523.   defsubr (&Sx_display_backing_store);
  4524.   defsubr (&Sx_display_save_under);
  4525. #if 0
  4526.   defsubr (&Sx_rebind_key);
  4527.   defsubr (&Sx_rebind_keys);
  4528.   defsubr (&Sx_track_pointer);
  4529.   defsubr (&Sx_grab_pointer);
  4530.   defsubr (&Sx_ungrab_pointer);
  4531. #endif
  4532. #else
  4533.   defsubr (&Sx_get_default);
  4534.   defsubr (&Sx_store_cut_buffer);
  4535.   defsubr (&Sx_get_cut_buffer);
  4536. #endif
  4537.   defsubr (&Sx_parse_geometry);
  4538.   defsubr (&Sx_create_frame);
  4539.   defsubr (&Sfocus_frame);
  4540.   defsubr (&Sunfocus_frame);
  4541. #if 0
  4542.   defsubr (&Sx_horizontal_line);
  4543. #endif
  4544.   defsubr (&Sx_open_connection);
  4545.   defsubr (&Sx_close_current_connection);
  4546.   defsubr (&Sx_synchronize);
  4547. }
  4548.  
  4549. #endif /* HAVE_X_WINDOWS */
  4550.